## Tuesday, August 23, 2011

### Adding two complex numbers using Structure in C

Simple algebraic addition do no work in the case of Complex Number. Because they have two parts, Real and Imaginary. To add two complex numbers, real part of one number must be added with real part of other and imaginary part one must be added with imaginary part of other. The concept of complex number can be viewed as 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\n", result.real,result.imag);    return 0;}

Output

#### 1 comment:

1. 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).