-->

Program to compute the sum of two complex numbers.

C Program to compute the sum of two complex numbers
Program to compute the sum of two complex numbers.

#include <stdio.h>
typedef struct complex
{
float real;
float imag;
}complex;
complex add(complex nl, complex n2);
int main()
{
complex nl,n2,temp; // Declaring variables of type 'complex'
// Reading values into members of the 'complex' type variables
printf("For 1st complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%f%f",&n1.real,&n1.imag);
printf("\nFor 2nd complex number \n");
printf("Enter real and imaginary respectively:\n");
scanf("%r/of", &n2.real, &n2.imag);
// Calling the add functions with two 'complex' type parameters
temp = add(n1,n2);
printf("Sum=%.1f + %.1fi", temp.real, temp.imag);
return 0;
}
/* Defining add function */
complex add(complex n1,complex n2
{
complex temp;
temp.real = n1.real + n2.real;
temp.imag = n1.imag + n2.imag;
return(temp);
}







We can pass variables of a structure type to functions and return a structure type too












n1,n2àn1 and n2, two 'complex' type parameters are passed to 'add'
temp à'add' returns a 'complex' type value

'add' function takes two 'complex' type parameters and returns 'complex' type
Output:
For 1st complex number
Enter real and imaginary respectively:
12.25
25

For 2nd complex number
Enter real and imaginary respectively:
12.25
25
some = 24.5 + 50.0

Related Posts

Subscribe Our Newsletter