C program to add two complex numbers:
#include <stdio.h>
struct complex
{
int real, img;
};
int main()
{
struct complex a, b, c;
printf(“Please enter first complex number\n”);
printf(“Enter Real part of the 1st complex number\n”);
scanf(“%d”, &a.real);
printf(“Enter Imaginary part of the 1st complex number without i\n”);
scanf(“%d”, &a.img);
printf(“Please enter second complex number\n”);
printf(“Enter Real part of the 2nd complex number\n”);
scanf(“%d”, &b.real);
printf(“Enter Imaginary part of the 2nd complex number without i\n”);
scanf(“%d”, &b.img);
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf(“The sum of two complex numbers = %d + %di\n”,c.real,c.img);
else
printf(“The sum of two complex numbers = %d %di\n”,c.real,c.img);
return 0;
}
Output:
Please enter first complex number
Enter Real part of the 1st complex number
2
Enter Imaginary part of the 1st complex number without i
4
Please enter second complex number
Enter Real part of the 2nd complex number
6
Enter Imaginary part of the 2nd complex number without i
8
The sum of two complex numbers = 8 + 12i