Adding two complex numbers using Structure in C
Simple algebraic addition does not work in the case of Complex Number. Because they have two parts, Real and Imaginary. To add two complex numbers, a real part of one number must be added with a real part of other and imaginary part one must be added with an imaginary part of other. The concept of complex number can be viewed as a structure having two members real and imaginary. So to add two complex numbers we use structure addition. If complex1 and complex2 be two structure variables and complex3 be their sum then
complex3.real = complex1.real + complex2.real; complex3.imag = complex1.imag + complex2.imag;
In this way, we can add two complex numbers. The source code and output of the program is given here…
Source Code:
#include<stdio.h> struct comp{ float real; float imag; }; struct comp comp1,comp2; struct comp sum_complex(struct comp complex1,struct comp complex2){ struct comp temp; temp.real = complex1.real + complex2.real; temp.imag = complex1.imag + complex2.imag; return temp; } int main(){ struct comp result; printf("Enter Complex Number 1: "); scanf("%f%f",&comp1.real, &comp1.imag); printf("Enter Complex Number 2: "); scanf("%f%f",&comp2.real,&comp2.imag); result = sum_complex(comp1,comp2); printf("The sum is %.2f + i%.2f\n", result.real,result.imag); return 0; }
Output
I am here to discuss about a number which can be put in the form a + bi termed as complex number, where a and b are real numbers and i is called the imaginary unit,in given expression "a" is the real part and b is the imaginary part of the complex number. The complex number can be identified with the point (a, b).