Thursday 13 September 2012

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


Description:
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.
Algorithm:
Step 1: Start
Step 2: declare structure for complex numbers
Step 3: read the complex number
Step 4: read choice
Step 5: if choice=1 then addition operation will perform and it contains following           
             steps
                           i)  w.realpart = w1.realpart+w2.realpart;
                        ii) w.imgpart = w1.imgpart+w2.imgpart; goto step 4
Step 6: if choice=2 then multiplication operation will perform and it contains       
                following  steps
                      i)  w.realpart=(w1.realpart*w2.realpart)-(w1.imgpart*w2.imgpart);
ii) w.imgpart=(w1.realpart*w2.imgpart)+(w1.imgpart*w2.realpart); goto step 4
Step 7: if choice=0 then exit operation will perform 


Step 8:if w.imgpart>0 then print realpart+imgpart else
                         Print realpart.
Step 9: Stop
 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 Real 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 QUESATIONS:

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.

No comments:

Post a Comment