#include <stdio.h>
struct complex {
float real;
float imag;
};
struct complex add(struct complex c1, struct complex c2);
int main() {
struct complex num1, num2, result;
printf("Enter first complex number:\n");
printf("Real part: ");
scanf("%f", &num1.real);
printf("Imaginary part: ");
scanf("%f", &num1.imag);
printf("\nEnter second complex number:\n");
printf("Real part: ");
scanf("%f", &num2.real);
printf("Imaginary part: ");
scanf("%f", &num2.imag);
result = add(num1, num2);
printf("\nSum = %.1f + %.1fi\n", result.real, result.imag);
return 0;
}
struct complex add(struct complex c1, struct complex c2) {
struct complex temp;
temp.real = c1.real + c2.real;
temp.imag = c1.imag + c2.imag;
return temp;
}
Enter first complex number:
Real part: 3.6
Imaginary part: 4.7
Enter second complex number:
Real part: 2.9
Imaginary part: 6.4
Sum = 6.5 + 11.1i
complex
that contains two members : real
and imag
. The program also defines a function add
that takes two complex
structures as arguments, performs the addition of the two complex numbers, and returns a complex
structure containing the sum.complex
structure called num1
, num2
, and result
, initializes num1
and num2
with input values from the user using the scanf function
, calls the add function to add num1
and num2
and store the result in result
, and prints
the sum of the complex
numbers using the printf function
.