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.
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 .
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 : :
OUTPUT :
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 .
need an explanation for this answer? contact us directly to get an explanation for this answer