C++ program to add subtract and multiply two complex numbers

//C++ Program to add subtract and Multiply Complex Numbers
#include<iostream>
using namespace std;
#include<math.h>

//Declare structure
   struct complex
   {
     int real,img;
   };

   int main()
   {
      complex a,b,c;
      int choice;
   cout<<"Enter First Complex Number:"<<endl;
   cout<<"Enter Real Part of First Complex No=";
   cin>>a.real;
   cout<<"Enter imaginary Part of First Complex No.=";
   cin>>a.img;
   cout<<endl;
   cout<<"Enter Second Complex Number:"<<endl;
   cout<<"Enter Real Part of Second Complex No =";
   cin>>b.real;
   cout<<"Enter imaginary Part of Second Complex No.= ";

   cin>>b.img;

   cout<<endl;
   cout<<"\tEnter 1 to add two complex number \n";
   cout<<"\tEnter 2 to subtract two complex numbers\n";
   cout<<"\tEnter 3 to multiply two complex numbers\n";
   cout<<"Enter your choice ";
   cin>>choice;
   switch(choice)
    {
     case 1:
       {
      //add real with real
      c.real=a.real+b.real;
      //add imaginary with imaginary
      c.img=a.img+b.img;
      cout<<"********************************************************"<<endl;

      //to check whether the imaginary part is positive or negative
      if (c.img<0)
      cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<c.img<<"i"<<endl;

      else
      cout<<"\nThe Sum Of 1st and 2nd Complex No. = "<<c.real<<"+"<<c.img<<"i"<<endl;
      break;
         }
  case 2:
      {
      c.real=a.real-b.real;
      c.img=a.img-b.img;

      if (c.img<0)
      {
        cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
      }
      else
       cout<<"\nThe Difference of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl;
       break;
     }
  case 3:
    {
   //multipication of complex Nos start here

      c.real=(a.real*b.real+(a.img*b.img)*-1);
      c.img=a.img*b.real+b.img*a.real;

      if (c.img<0)
         {
         cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<c.img<<"i"<<endl;
         }
      else
       cout<<"\nThe Product of 1st and 2nd Complex No is = "<<c.real<<"+"<<c.img<<"i"<<endl<<endl;
            break;
      }
    }
   return 0;
 }

No comments:

Post a Comment