C program to add, subtract and multiply two complex numbers using structure

C Programming Computer Science
#include<conio.h>
#include<stdio.h>
#include<stdio.h>
struct complex
{
int real, img;
};
int main()
{
int choice;
struct complex a,b,c;
while(1)
{
printf("Press 1 to add two complex numbers.\n");
printf("Press 2 to subtract two complex numbers.\n");
printf("Press 3 to multiply two complex numbers.\n");
printf("Press 4 to exit.\n");
printf("Enter your choice.\n");
scanf("%d",&choice);

if(choice==4)
exit(0);

if(choice>=1 && choice<=3)
{
printf("Enter a and b where a+ib is the first complex number");
printf("\na=");
scanf("%d", &a.real);
printf("b=");
scanf("%d", &a.img);
printf("Enter c and d where c+id is the second complex number");
printf("\nc=");
scanf("%d", &b.real);
printf("d=");
scanf("%d", &b.img);
}

if(choice==1)
{
c.real=a.real+b.real;
c.img=a.img+b.img;
if(c.img>=0)
printf("Sum of two complex numbers=%d+%di",c.real,c.img);
else
printf("Sum of two complex numbers=%d%di", c.real,c.img);
}
else if(choice==2)
{
c.real=a.real-b.real;
c.img=a.img-b.img;
if(c.img>=0)
printf("Difference of two complex numbers=%d+%di",c.real,c.img);
else
printf("Difference of two complex numbers=%d%di", c.real,c.img);
}

else if(choice==3)
{
c.real=a.real*b.real-a.img*b.img;
c.img=a.img*b.real+a.real*b.img;

if(c.img>=0)
printf("Multiplication of two complex numbers=%d+%di",c.real,c.img);
else
printf("Multiplucation of two complex numbers=%d%di", c.real,c.img);
}
else
printf("Invalid Choice");
printf("\nPress any key to enter choice again....\n");
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.