Write a programme in 'C'. Define a structure “complex” to read two complex numbers and perform addition, subtraction of these two complex num
1. Write a programme in 'C'. Define a structure “complex” to read two complex numbers and perform
addition, subtraction of these two complex numbers and display the result.
Code:
#include<stdio.h>
struct complex{
float real;
float img;
};
int main(){
struct complex c1,c2,add,sub;
printf("Enter a and b of the first complex number (a+ib) \n");
scanf(" %f %f",&c1.real,&c1.img);
printf("Enter a and b of the second complex number (a+ib) \n");
scanf(" %f %f",&c2.real,&c2.img);
add.real = c1.real+c2.real;
add.img = c1.img+c2.img;
sub.real = c1.real-c2.real;
sub.img = c1.img-c2.img;
printf("c1+c2 = %f + %fi\n",add.real,add.img);
if(sub.img<0) printf("c1-c2 = %f %fi",sub.real,sub.img);
else printf("c1-c2 = %f + %fi",sub.real,sub.img);
return 0;
}
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 1 images