11. CREATE A STRUCTURE COMPLEX NUMBER HAVING REAL AND
IMAGINARY PART AS PROPERTIES.
WRITE FUNCTIONS TO ADD AND SUBTRACT THE TWO COMPLEX
NUMBERS.
SOURCE CODE:
#include <stdio.h>
struct complex
{
int real, img;
};
struct complex a, b, c;
void readcom(){
printf("Enter a and b where a + ib is the first complex
number.\n");
printf("a = ");
scanf("%d", &a.real);
printf("b = ");
scanf("%d", &a.img);
printf("Enter c and d where c + id is the second
complex number.\n");
printf("c = ");
scanf("%d", &b.real);
printf("d = ");
scanf("%d", &b.img);
}
void addcom(){
c.real = a.real + b.real;
c.img = a.img + b.img;
if ( c.img >= 0 )
printf("Sum of two complex numbers = %d +
%di\n",c.real,c.img);
else
printf("Sum of two complex numbers = %d
%di\n",c.real,c.img);
}
void subcom(){
c.real = a.real - b.real;
c.img = a.img - b.img;
if ( c.img >= 0 )
printf("\nDifference of two complex numbers = %d +
%di",c.real,c.img);
else
printf("\nDifference of two complex numbers = %d
%di",c.real,c.img);
}
void main()
{
readcom();
addcom();
subcom();
}
0 comments:
Post a Comment