# Add Two Complex Numbers by Passing Structure with Function

The Structure is a user-defined data type in C language that allows us to combine data of different types together. Structure helps to construct a complex data type that is more meaningful. It is somewhat similar to an Array, but an array holds data of similar type only. But the structure, on the other hand, can store data of any type, which is practical more useful.

## What are Complex Numbers?

Complex numbers can be identified by their form, It is classified into two parts real part and imaginary part. Complex numbers are of the form X + Y, Here X is known as the real part, and Y is known as the imaginary part.

The User has to give two complex numbers as structure members and perform the operation on it by using the user-defined function, to add the complex number user has to add the real and imaginary parts respectively.

**For example, 5+2i and 7 +4i are 12+6i.**

## The Algorithm for Adding The Complex Numbers is:

**Define a structure "Complex" with its data members,****Define variable and its type,****Get the input(Imaginary part & Real part) from the user,****By calling the function, Add the real and imaginary parts,****Display the result on the print screen.**

## C Program to Add two Compex Numbers by using the function:

```
#include <stdio.h>
typedef struct complex {
float real;
float imag;
} 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.imag);
printf("\nFor 2nd complex number \n");
printf("Enter the real and imaginary parts: ");
scanf("%f %f", &n2.real, &n2.imag);
result = add(n1, n2);
printf("Sum = %.1f + %.1fi", result.real, result.imag);
return 0;
}
complex add(complex n1, complex n2) {
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return (temp);
}
```

- Here in this programm, The member function of the complex structure is
*imag*and*real*. The complex structure is defined with two variables, n1,n2. - The values for the two variables are obtained from the user and sent to the add function, The function will compute the operation and store the value in the temp variable.

## Output:

For 1st complex number

Enter the real and imaginary parts: 24 3

For 2nd complex number

Enter the real and imaginary parts: 45 32

Sum = 69.0 + 35.0i