You are here

C program to add two complex numbers

C program to add two complex numbers: this program performs addition of two complex numbers which will be entered by a user and then prints it. A user inputs real and imaginary parts of two complex numbers. In our program we will add real parts and imaginary parts of complex numbers and prints the complex number, 'i' is the symbol used for iota. For example, if a user inputs two complex numbers as (1 + 2i) and (4 + 6 i) then the output of the program will be (5 + 8i). A structure is used to store a complex number.

Complex numbers program in C language

  1. #include <stdio.h>
  3. struct complex
  4. {
  5.    int real, img;
  6. };
  8. int main()
  9. {
  10.    struct complex a, b, c;
  12.    printf("Enter a and b where a + ib is the first complex number.\n");
  13.    scanf("%d%d", &a.real, &a.img);
  14.    printf("Enter c and d where c + id is the second complex number.\n");
  15.    scanf("%d%d", &b.real, &b.img);
  17.    c.real = a.real + b.real;
  18.    c.img = a.img + b.img;
  20.    printf("Sum of the complex numbers: (%d) + (%di)\n", c.real, c.img);
  22.    return 0;
  23. }

Download add complex numbers program executable.

C program to add two complex numbers output:
Output of C program to add two complex numbers