# How to add two complex numbers by passing structure to a function in C language?

CServer Side ProgrammingProgramming

In order to add two complex numbers in C programming language, the user has to take two complex numbers as structure members and perform addition operation on those two numbers by creating a user-defined function.

## Algorithm

Refer an algorithm given below for addition of two complex numbers.

Step 1: Declare struct complex with data members.
Step 2: Declare name for structure and variables.
Step 3: Enter real and imaginary part for first complex number at run time.
Step 4: Enter real and imaginary part for second complex number at runtime
Step 5: Compute addition of number1 and number2 by calling function. Go to step 7.
Step 6: Print the result.
Declare temp variabletemp.real = num1.real + num2.real;temp.imag = num1.imag + num2.imag;return (temp);

## Example

Following is the C program for adding two complex numbers by passing structure to a function −

Live Demo

#include <stdio.h>
typedef struct complex{
float real;
float imag;
} complex;
int main(){
complex num1, num2, value;
printf("entering real and imag parts of first complex no:\n ");
scanf("%f %f", &num1.real, &num1.imag);
printf("entering real and imag parts of second complex no:\n ");
scanf("%f %f", &num2.real, &num2.imag);
printf("result = %.1f + %.1fi", value.real, value.imag);
return 0;
}
complex temp;
temp.real = num1.real + num2.real;
temp.imag = num1.imag + num2.imag;
return (temp);
}

## Output

When the above program is executed, it produces the following result −

entering real and imag parts of first complex no:
entering real and imag parts of second complex no:
result = 0.0 + 0.0i
Published on 24-Mar-2021 12:25:20