Tuesday, September 18, 2012

Code #95:c program to add two complex numbers

| |
#include <stdio.h>

struct complex
{
   int real, img;
};

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 + %di\n",c.real,c.img);
   else
      printf("Sum of two complex numbers = %d %di\n",c.real,c.img);

   return 0;
}
Enter a and b where a + ib is the first complex number
a = 4
b = 5
Enter c and c where c + id is the second complex number
c = 2
d = 6
Sum of two complex numbers =6 + 11i

0 comments:

Post a Comment

Powered by Blogger.