PROGRAM TO ADD TWO COMPLEX NUMBERS

 In this example , you will  learn a program to add two complex numbers by passing structure to function.


(To understand this program , you should have prior knowledge about complex numbers and its addition .)


Program:

//Program to add two complex numbers

#include<stdio.h> 
typedef struct complex {
float real;
float imagi;
} complex;

complex add(complex n1, complex n2);

int main( )
{
complex n1, n2, result;

printf("For 1st complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n1.real, &n1.imagi);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imagi);

result = add(n1, n2);

printf("Sum of complex numbers = %.1f + %.1fi", result.real, result.imagi);
return 0;
}

complex add(complex n1, complex n2)

{
complex temp;
temp.real = n1.real + n2.real;
temp.imagi = n1.imagi + n2.imagi;
return (temp);
}

Output:


For 1st complex number 
Enter the real and imaginary parts:7.9      1.2

    
For 2nd complex number    
Enter the real and imaginary parts:23.2        4.5
    
Sum of complex numbers= 31.1 + 5.7i



So that's it ! Just try this code by your own. If you have any doubts then feel free to drop a comment . I'll be happy to answer your questions .

Keep coding....

Comments

Popular posts from this blog

DECIMAL TO OCTAL CONVERSION IN C

PROGRAM TO CONVERT BINARY CODE TO GRAY CODE

KRISHNMURTHY NUMBER PROGRAM IN C