C Program to Add Two Complex Numbers

  • Write a program in c to add two complex numbers.

complex number is a number that can be expressed in the form a + bi, where a and b are real numbers and i is the imaginary unit, which satisfies the equation i2 = -1. In complex number a + bi, a is the real part and b is the imaginary part. For example: (2 + 3i) is a complex number.

Complex Number Arithmetic

Addition of Complex Numbers

Consider two complex numbers C1 = (a + ib) and C2 = (c + id)
Let Sum(x + iy) is the sum of C1 and C2
 Sum = C1 + C2
 (x + iy) = (a + ib) + (c + id)
 (x + iy) = (a + c) + i(b + d)
 x = (a + c) and, 
 y = (b + d)

Subtraction of Complex Numbers

Let Diff(x + iy) is the difference of C1 and C2
 Diff = C1 - C2
 (x + iy) = (a + ib) - (c + id)
 (x + iy) = (a - c) + i(b - d)
 x = (a - c) and, 
 y = (b - d)

C program to add two complex numbers using structures

Here we are using a user defined structure ‘complexNumber’ that contains two integer data members to store real and imaginary part of complex number. A structure provides an encapsulation to represent a complex number as an single entity instead of using two variables to store a complex number.

In this program we take two complex numbers as input from user in the form of A + iB. We will add real parts of input complex number to get the real part of sum complex and add imaginary part of input complex to get imaginary part of sum complex number. Then we print sum complex number.

C program to add two complex numbers using structures 1

/*
* C Program to add two complex numbers 
*/
#include <stdio.h>
#include <conio.h>
 
/* Structure to store complex number in the form of x + yi, 
 * realPart = x and imaginaryPart = y;
 */
typedef struct complex
{
   int realPart;
   int imaginaryPart;
} complexNumber;
  
int main()
{
   complexNumber firstCN, secondCN, sumCN;
  
   printf("Enter value of A and B  where A + iB is first complex number\n");
   scanf("%d %d", &firstCN.realPart, &firstCN.imaginaryPart);
    
   printf("Enter value of C and D  where C + iD is second complex number\n");
   scanf("%d %d", &secondCN.realPart, &secondCN.imaginaryPart);
    
   /* (A + Bi) + (C + Di) = (A+C) + (B+D)i */
   sumCN.realPart = firstCN.realPart + secondCN.realPart;
   sumCN.imaginaryPart = firstCN.imaginaryPart + secondCN.imaginaryPart;
     
  
   if (sumCN.imaginaryPart >= 0 )
      printf("SUM = %d + %di\n", sumCN.realPart, sumCN.imaginaryPart);
   else
      printf("SUM = %d %di\n", sumCN.realPart, sumCN.imaginaryPart);
    
   getch();
   return 0;
}

Program Output

Enter value of A and B where A + iB is first complex number
2 3
Enter value of C and D where C + iD is second complex number
3 4
SUM = 5 + 7i

C program to subtract two complex numbers using structures

C program to subtract two complex numbers using structures 1

/*
* C Program to Subtract two complex numbers
*/
#include <stdio.h>
#include <conio.h>
  
/* Structure to store complex number in the form of x + yi,
 * realPart = x and imaginaryPart = y;
 */
typedef struct complex
{
   int realPart;
   int imaginaryPart;
} complexNumber;
  
int main()
{
   complexNumber firstCN, secondCN, sumCN;
  
   printf("Enter value of A and B  where A + iB is first complex number\n");
   scanf("%d %d", &firstCN.realPart, &firstCN.imaginaryPart);
    
   printf("Enter value of C and D  where C + iD is second complex number\n");
   scanf("%d %d", &secondCN.realPart, &secondCN.imaginaryPart);
    
   /* (A + Bi) - (C + Di) = (A-C) + (B-D)i */
   sumCN.realPart = firstCN.realPart - secondCN.realPart;
   sumCN.imaginaryPart = firstCN.imaginaryPart - secondCN.imaginaryPart;
    
  
   if (sumCN.imaginaryPart >= 0 )
      printf("DIFFERENCE = %d + %di\n", sumCN.realPart, sumCN.imaginaryPart);
   else
      printf("DIFFERENCE = %d %di\n", sumCN.realPart, sumCN.imaginaryPart);
    
   getch();
   return 0;
}

Program Output

Enter value of A and B where A + iB is first complex number
5 4
Enter value of C and D where C + iD is second complex number
2 2
DIFFERENCE = 3 + 2i