Saturday 18 December 2010

Program for To read the two complex numbers and perform the addition and multiplication of these two numbers

In this program the complex number means it contains the two parts . first one

is real part and second one is imaginary part(2+3i).by taking these two complex numbers we can perform the addition and multiplication operation.


Program:

#include<stdio.h>
#include<math.h>
void arithmetic(int opern); struct comp
{
double realpart; double imgpart;

};
void main()
{
int opern; clrscr();

printf("\n\n \t\t\t***** MAIN MENU *****");
printf("\n\n Select your option: \n 1 : ADD\n 2 : MULTIPLY\n 0 : EXIT \n\n\t\t Enter your Option [ ]\b\b");
scanf("%d",&opern);
if(opern>2)
{
printf("invalid option");
}
else
{
switch(opern)
{
case 0: exit(0); case 1:
case 2:

arithmetic(opern);
  default:
 main();
}
}
getch();
}

void arithmetic(int opern)
{
struct comp w1, w2, w;
printf("\n Enter two Complex Numbers (x+iy):\n
eal Part of First Number:");
scanf("%lf",&w1.realpart);
printf("\n Imaginary Part of First Number:");
scanf("%lf",&w1.imgpart);
printf("\n Real Part of Second Number:"); 
scanf("%lf",&w2.realpart);
printf("\n Imaginary Part of Second Number:"); scanf("%lf",&w2.imgpart);
switch(opern)
{
/*addition of complex number*/
 case 1:
 w.realpart=w1.realpart+w2.realpart; w.imgpart=w1.imgpart+w2.imgpart; break;

/*multiplication of complex number*/
case 2:
w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart); w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart); break;

}
if (w.imgpart>0)
printf("\n Answer = %lf+%lfi",w.realpart,w.imgpart); else
printf("\n Answer = %lf%lfi",w.realpart,w.imgpart); 
getch();
main();
}

Output:

***** MAIN MENU *****

Select your option: 
1 : ADD
2 : MULTIPLY
0 : EXIT

Enter your Option [ 1]

Enter two Complex Numbers (x+iy):
Real Part of First Number:2
Imaginary Part of First Number:2

Real Part of Second Number:2
Imaginary Part of Second Number:2

Answer = 4.000000+4.000000i
***** MAIN MENU *****
Select your option: 
1 : ADD
2 : MULTIPLY
0 : EXIT

Enter your Option [ 2]

Enter two Complex Numbers (x+iy):
Real Part of First Number:2
Imaginary Part of First Number:2

Real Part of Second Number:2
Imaginary Part of Second Number:2

Answer = 0.000000+8.000000i

***** MAIN MENU *****

Select your option: 
1 : ADD
2 : MULTIPLY
0 : EXIT

Enter your Option  3 
invalid option

***** MAIN MENU *****

Select your option:
 1 : ADD
2 : MULTIPLY
0 : EXIT

Enter your Option [ 0]

Conclusion: The program is error free

VIVA QUESTIONS:

1) Define structure ?
Ans: Structure is amethod for packing data of different types. Structure help to organize complex data in a more meaninigful way.

2) What is use of <math.h> header file ?
Ans: It is used to access the mathematical functions in programs.


Algorithm
program
c programs

No comments:

Post a Comment