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.

struct COMPLEX
{
    int REAL;
    int IMG;
};

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

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

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 :

  1. #include<stdio.h>
  2. struct COMPLEX
  3. {
  4.     int REAL;
  5.     int IMG;
  6. };
  7. void main()
  8. {
  9.     struct COMPLEX A,B,C;
  10.     clrscr();
  11.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
  12.     scanf("%d",&A.REAL);
  13.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
  14.     scanf("%d",&A.IMG);
  15.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
  16.     scanf("%d",&B.REAL);
  17.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
  18.     scanf("%d",&B.IMG);
  19.     C.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
  20.     C.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
  21.     printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
  22.     printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG\
  23.     ,B.REAL,B.IMG,C.REAL,C.IMG);
  24.     getch();
  25. }

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.

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

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.

struct COMPLEX
{
    int REAL;
    int IMG;
};

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

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

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 :

  1. #include<stdio.h>
  2. struct COMPLEX
  3. {
  4.     int REAL;
  5.     int IMG;
  6. };
  7. void main()
  8. {
  9.     struct COMPLEX A,B,C;
  10.     struct COMPLEX multi_complex(struct COMPLEX,struct COMPLEX);
  11.     clrscr();
  12.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR A :");
  13.     scanf("%d",&A.REAL);
  14.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR A :");
  15.     scanf("%d",&A.IMG);
  16.     printf("\nENTER REAL PART OF COMPLEX NO. VALUES FOR B :");
  17.     scanf("%d",&B.REAL);
  18.     printf("\nENTER IMAGINARY PART OF COMPLEX NO. VALUES FOR B :");
  19.     scanf("%d",&B.IMG);
  20.     C=multi_complex(A,B);
  21.     printf("\nRESULT FOR COMPLEX NO. C VALUES ARE :\n");
  22.     printf("(%d + %di) * (%d + %di) = %d + %di",A.REAL,A.IMG,\
  23.     B.REAL,B.IMG,C.REAL,C.IMG);
  24.     getch();
  25. }
  26. struct COMPLEX multi_complex(struct COMPLEX A,struct COMPLEX B)
  27. {
  28.     struct COMPLEX R;
  29.     R.REAL=(A.REAL*B.REAL)-(A.IMG*B.IMG);
  30.     R.IMG=(A.REAL*B.IMG)+(A.IMG*B.REAL);
  31.     return R;
  32. }

SCREEN SHOTS:-

C_program_Complex_Function_Multiply

C_program_Complex_Function_Multiply_Output

Leave a Reply