C program to add two complex numbers

Created with Sketch.

 

C program to add two complex numbers

C program to add two complex numbers: this program performs the 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

#include <stdio.h>

struct complex
{
int real, img;
};

int main()
{
struct complex a, b, c;

printf(“Enter a and b where a + ib is the first complex number.\n);
scanf(“%d%d”, &a.real, &a.img);
printf(“Enter c and d where c + id is the second complex number.\n);
scanf(“%d%d”, &b.real, &b.img);

c.real = a.real + b.real;
c.img = a.img + b.img;

printf(“Sum of the complex numbers: (%d) + (%di)\n, c.real, c.img);

return 0;
}

 

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

Leave a Reply

Your email address will not be published. Required fields are marked *