17. write a C program to perform specified operation on complex numbers


Program to perform specified operation on complex numbers

Source Code:

#include<stdio.h>
typedef struct complex
{
double a;
double b;
}
complex;
complex add(complex c1,complex c2);
complex mul(complex c1,complex c2);
int main()
{
complex c1,c2,c3;
printf("for 1st complex number:\n");
printf("enter the real and imaginary parts:\n");
scanf("%lf%lf",&c1.a,&c1.b);
printf("for 2nd complex number:\n");
printf("enter the real and imaginary parts:\n");
scanf("%lf%lf",&c2.a,&c2.b);
c3=add(c1,c2);
printf("sum=%1lf+%1lf\n",c3.a,c3.b);
c3=mul(c1,c2);
printf("mul=%1lf*%1lf",c3.a,c3.b);
return 0;
}
complex add(complex c1, complex c2)
{
complex c3;
c3.a=c1.a+c2.a;
c3.b=c1.b+c2.b;
return (c3);
}
complex mul(complex c1,complex c2)
{
complex c3;
c3.a=(c1.a*c2.a)-(c1.b*c2.b);
c3.b=(c1.a*c2.a)+(c1.b*c2.b);
return (c3);
}

Output:

for 1st complex number:
enter the real and imaginary parts:
2.1
2.5
for 2nd complex number:
enter the real and imaginary parts:
1.6
3.8
sum=3.700000+6.300000
mul=-6.140000*12.860000

No comments:

Post a Comment