C program to Add two Complex Numbers using structures

Add two Complex Numbers using structures


Write a C program to Add two Complex Numbers using structures. Here’s simple C program to Add two Complex Numbers using structures in C Programming Language.


Numbers in C


Normally, when we work with Numbers, we use primitive data types such as int, short, long, float and double, etc. The number data types, their possible values and number ranges have been explained while discussing C Data Types.


Here is source code of the C program to Add two Complex Numbers using structures. The C program is successfully compiled and run(on Codeblocks) on a Windows system. The program output is also shown in below.


SOURCE CODE : :


/* C program to Add two Complex Numbers using structures  */

#include <stdio.h>

struct complex
{
   int real, img;
};

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

   printf("\nEnter first complex number :: \n");
   printf("\nEnter Real Part :: ");
   scanf("%d", &a.real);
   printf("\nEnter Imaginary Part :: ");
   scanf("%d", &a.img);

   printf("\nEnter Second complex number :: \n");
   printf("\nEnter Real Part :: ");
   scanf("%d", &b.real);
   printf("\nEnter Imaginary Part :: ");
   scanf("%d", &b.img);

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

   if ( c.img >= 0 )
      printf("\nSum of two complex numbers = %d + %di\n", c.real, c.img);
   else
      printf("\nSum of two complex numbers = %d %di\n", c.real, c.img);

   return 0;
}

OUTPUT : :


/* C program to Add two Complex Numbers using structures  */

Enter first complex number ::

Enter Real Part :: 2

Enter Imaginary Part :: 4

Enter Second complex number ::

Enter Real Part :: 3

Enter Imaginary Part :: 6

Sum of two complex numbers = 5 + 10i

Process returned 0

Above is the source code for C program to Add two Complex Numbers using structures which is successfully compiled and run on Windows System.The Output of the program is shown above .

If you found any error or any queries related to the above program or any questions or reviews , you wanna to ask from us ,you may Contact Us through our contact Page or you can also comment below in the comment section.We will try our best to reach up to you in short interval.


Thanks for reading the post….

Leave a Reply