C Program to Add Two Complex Numbers by Passing Structure to a Function
This program takes two complex numbers as structures and adds them with the use of functions.
Example: Add Two Complex Numbers
#include <stdio.h>
typedef struct complex
{
float real;
float imag;
} complex;
complex add(complex n1,complex n2);
int main()
{
complex n1, n2, temp;
printf("For 1st complex number \n");
printf("Enter real and imaginary part respectively:\n");
scanf("%f %f", &n1.real, &n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter real and imaginary part respectively:\n");
scanf("%f %f", &n2.real, &n2.imag);
temp = add(n1, n2);
printf("Sum = %.1f + %.1fi", temp.real, temp.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);
}
Output
For 1st complex number Enter real and imaginary part respectively: 2.3 4.5 For 2nd complex number Enter real and imaginary part respectively: 3.4 5 Sum = 5.7 + 9.5i
In this program, structures n1 and n2 are passed as an argument of the function add()
.
This function computes the sum and returns the structure variable temp to the main()
function.
C program that adds two complex numbers by passing a structure to a function:
#include <stdio.h>
struct complex {
float real;
float imag;
};
struct complex add(struct complex c1, struct complex c2) {
struct complex result;
result.real = c1.real + c2.real;
result.imag = c1.imag + c2.imag;
return result;
}
int main() {
struct complex c1, c2, result;
// Input for c1
printf("Enter real and imaginary parts of complex number 1:\n");
scanf("%f %f", &c1.real, &c1.imag);
// Input for c2
printf("Enter real and imaginary parts of complex number 2:\n");
scanf("%f %f", &c2.real, &c2.imag);
// Add the two complex numbers
result = add(c1, c2);
// Output
printf("Sum = %.2f + %.2fi", result.real, result.imag);
return 0;
}
Explanation:
#include <stdio.h>
includes the standard input-output header file in our program, which provides input and output operations functionality.struct complex {...};
defines a structure namedcomplex
which has two members:real
andimag
.struct complex add(struct complex c1, struct complex c2) {...}
defines a function namedadd()
which takes two complex numbers as input and returns their sum as a complex number.int main()
is the entry point of our program.struct complex c1, c2, result;
declares three variablesc1
,c2
, andresult
of typestruct complex
.- The next four statements use
printf()
andscanf()
to input the real and imaginary parts of the two complex numbers. result = add(c1, c2);
calls theadd()
function and stores its return value inresult
.printf("Sum = %.2f + %.2fi", result.real, result.imag);
prints the sum of the two complex numbers usingprintf()
and the%f
and%i
format specifiers. The.2
in%.2f
specifies that only two decimal places should be printed.
When we compile and run this program, it will prompt the user to input the real and imaginary parts of two complex numbers and then display their sum.