Thursday 21 May 2015

Addition subtraction of complex numbers using structure

#include<stdio.h>
struct complex
{
int real;
int img;
};

void display(struct complex c)
{
printf("%d",c.real);
if(c.img>=0)
{
printf("+%di",c.img);
}else
printf("%di",c.img);
}
void add(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
display(c3);
}
void sub(struct complex c1,struct complex c2)
{
struct complex c3;
c3.real=c1.real-c2.real;
c3.img=c1.img-c2.img;
display(c3);
}
main()
{
struct complex c1,c2;
printf("Enter the values of 1st complex number\n");
printf("Enter real part :");
scanf("%d",&c1.real);
printf("Enter imaginary part :");
scanf("%d",&c1.img);
printf("Enter the values of 2nd complex number\n");
printf("Enter real part :");
scanf("%d",&c2.real);
printf("\nEnter imaginary part :");
scanf("%d",&c2.img);
printf("\nC1=");
display(c1);
printf("\nC2=");
display(c2);
printf("\nC1 + C2 = ");
add(c1,c2);
printf("\nC1 - C2 = ");
sub(c1,c2);
getch();
}

No comments:

Post a Comment