Program to add two complex numbers in C

#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");
   printf("a = ");
   scanf("%d", &a.real);
   printf("b = ");
   scanf("%d", &a.img);
   printf("Enter c and d where c + id is the second complex number.n");
   printf("c = ");
   scanf("%d", &b.real);
   printf("d = ");
   scanf("%d", &b.img);

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

   if ( c.img >= 0 )
      printf("Sum of two complex numbers = %d + %din",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %din",c.real,c.img);

   return 0;
}

Output
Enter a and b where a + ib is the first complex number.
a=3
b=1
Enter c and d where c + id is the second complex number.
c=5
d=7
Sum of two complex numbers = 8+ 8i

Discover more from PlusDigit

Subscribe to get the latest posts sent to your email.

One thought on “Program to add two complex numbers in C

  1. Pingback: ray ban aviateur

Comments are closed.

Discover more from PlusDigit

Subscribe now to keep reading and get access to the full archive.

Continue reading

AdBlock Detected!

Please disable your AdBlocker to access this website.

Once disabled, this message will disappear automatically.