Write a C program that uses functions to perform the following operations:
i. Reading a complex number
ii. Writing a complex number
iii. Addition and subtraction of two complex numbers
iv. Multiplication of two complex numbers. Note: represent complex number using a structure.

Program Code:
#include<stdio.h>
#include<string.h>
struct complex1
{
int r;
inti;
}c1,c2,c3;
void add(struct complex1 c1,struct complex1 c2);
void sub(struct complex1 c1,struct complex1 c2);
void multi(struct complex1 c1,struct complex1 c2);
void main()
{
printf("\n enter first complex no: ");
scanf("%d%d",&c1.r,&c1.i);
printf("\n enter second complex no: ");
scanf("%d%d",&c2.r,&c2.i);
printf("\n enter first complex no:%d+i%d",c1.r,c1.i);
printf("\n enter second complex no:%d+i%d",c2.r,c2.i);
add(c1,c2);
sub(c1,c2);
multi(c1,c2);
}
void add(struct complex1 c1,struct complex1 c2)
{
c3.r=c1.r+c2.r;
c3.i=c1.i+c2.i;
printf("\n addition of two complex numbers is %d+i%d",c3.r,c3.i);
}
void sub(struct complex1 c1,struct complex1 c2)
{
c3.r=c1.r-c2.r;
c3.i=c1.i-c2.i;
printf("\n subtraction of two complex numbers is %d+i%d",c3.r,c3.i);
}
void multi(struct complex1 c1,struct complex1 c2)
{
c3.r=(c1.r*c2.r)-(c1.i*c2.i);
c3.i=(c1.r*c2.i)+(c1.i*c2.r);
printf("\n multiplication of two complex numbers is %d+i%d",c3.r,c3.i);
}
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..

Post A Comment:

0 comments: