C027 A C program that multiply two complex numbers

By | October 15, 2013

Let’s identify variables needed for the structure inside the program.

In this complex structure, we need two variables to store real part and imaginary part of complex number.

First variable will be the one which will save the real part of complex number and it will be REAL. Second variable will be the one which will save the imaginary part of complex number and it will be IMG, So in all Two variables.
The identified variables are REAL, IMG.

[codesyntax lang=”c”]

struct COMPLEX
{
    int REAL;
    int IMG;
};

[/codesyntax]

Now, Selection of data type will be int data type due to the values expected for complex number are decimals and they will be hold numbers so int data type is not sufficient for REAL, IMG.

Let’s identify variables needed for this program.

In this program, we need three variables to store two complex numbers entered by the user and one to store resultant complex number.

The identified variables are A, B, C data type will be same as the user defined data type struct COMPLEX.

Formula :- (a + bi) * (c + di) = (ac – bd) + (ad + bc)i

[codesyntax lang=”c”]

    C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);

[/codesyntax]

In this program, Give message to user to enter the required complex numbers and scan the entered numbers i.e. REAL, IMG. Calculate the multiplication of the two complex numbers. Finally printing of the result at the end of the program.

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
struct COMPLEX
{
    int REAL;
    int IMG;
};
void main()
{
    struct COMPLEX A,B,C;
    clrscr();
    printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
    scanf("%d",&A.REAL);
    printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
    scanf("%d",&A.IMG);
    printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
    scanf("%d",&B.REAL);
    printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
    scanf("%d",&B.IMG);
    C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
    printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
    printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG\
    ,B.REAL,B.IMG,C.REAL,C.IMG);
    getch();
}

[/codesyntax]

SCREEN SHOTS:-

C_program_Complex_Struct_Multiply

C_program_Complex_Struct_Multiply_Output

Now split the program to create a function by passing two structure complex variables to it. and return structure complex variable back.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

[codesyntax lang=”c”]

    struct COMPLEX A,B,C;
    struct COMPLEX multi_complex(struct COMPLEX,struct COMPLEX);

[/codesyntax]

Let’s identify variables needed for the structure inside the program.

In this complex structure, we need two variables to store real part and imaginary part of complex number.

First variable will be the one which will save the real part of complex number and it will be REAL. Second variable will be the one which will save the imaginary part of complex number and it will be IMG, So in all Two variables.
The identified variables are REAL, IMG.

[codesyntax lang=”c”]

struct COMPLEX
{
    int REAL;
    int IMG;
};

[/codesyntax]

Now, Selection of data type will be int data type due to the values expected for complex number are decimals and they will be hold numbers so int data type is not sufficient for REAL, IMG.

Let’s identify variables needed for this program.

In this program, we need three variables to store two complex numbers entered by the user and one to store resultant complex number.

The identified variables are A, B, C data type will be same as the user defined data type struct COMPLEX.

Formula :- (a + bi) * (c + di) = (ac – bd) + (ad + bc)i

[codesyntax lang=”c”]

    C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);

[/codesyntax]

In this program, Give message to user to enter the required complex numbers and scan the entered numbers i.e. REAL, IMG. Calculate the multiplication of the two complex numbers. Finally printing of the result at the end of the program.

C program code :

[codesyntax lang=”c” lines=”normal”]

#include<stdio.h>
struct COMPLEX
{
    int REAL;
    int IMG;
};
void main()
{
    struct COMPLEX A,B,C;
    struct COMPLEX multi_complex(struct COMPLEX,struct COMPLEX);
    clrscr();
    printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
    scanf("%d",&A.REAL);
    printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
    scanf("%d",&A.IMG);
    printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
    scanf("%d",&B.REAL);
    printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
    scanf("%d",&B.IMG);
    C=multi_complex(A,B);
    printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
    printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG,\
    B.REAL,B.IMG,C.REAL,C.IMG);
    getch();
}
struct COMPLEX multi_complex(struct COMPLEX A,struct COMPLEX B)
{
    struct COMPLEX R;
    R.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
    R.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
    return R;
}

[/codesyntax]

SCREEN SHOTS:-

C_program_Complex_Function_Multiply

C_program_Complex_Function_Multiply_Output

 

Note:- To understand program for sequence in detail Please SEARCH numerically example: C001, C002, etc.

Leave a Reply