"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Monday, January 18, 2010

Declare a structure to represent a complex no. ( a no. having a real and imaginary part). Write C++ function to add, subtract, multiply and divide two complex nos.


#include<iostream.h>
#include<conio.h>


struct complexNo
{
float real;
float img;
};
typedef complexNo cm;


void main()
{
cm n1, n2, res;


cout<<"\nEnter 2 complex nos:";


cout<<"\n\n Enter 1st No.:";


cout<<"\n Real = ? ";


cin>>n1.real;


cout<<"\n Imaginary = ? ";


cin>>n1.img;


cout<<"\n Enter 2st No.:";


cout<<"\n Real = ? ";


cin>>n2.real;


cout<<"\n Imaginary = ? ";


cin>>n2.img;

 res.real = n1.real +n2.real;


res.img = n1.img + n2.img;


cout<<"\n Sum of " << n1.real << " + i " << n1.img << " & " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img;


res.real = n1.real -n2.real;


res.img = n1.img - n2.img;


cout<<"\n Diff. of " << n1.real << " + i " << n1.img << " & " << n2.real << " + res.img; 


res.real = n1.real *n2.real;
res.img = n1.img *n2.img;

cout<<"\n Product of " << n1.real << " + i " << n1.img << " & " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img; 


res.real = n1.real / n2.real;
res.img = n1.img / n2.img;


cout<<"\n Result od dividing of " << n1.real << " + i " << n1.img << " by " << n2.real << " +i " << n2.img << "is "<< res.real << " + i " + << res.img; 


getch();
}


1 comment:

  1. #include

    struct complex
    {
    int real,img;

    } num1,num2;

    void add(complex,complex);
    void subtract(complex,complex);
    void multiply(complex,complex);
    void divide(complex,complex);


    void main ()
    {
    int x;
    cout<<"Enter 1st complex num.(real imaginary) ";
    cin>>num1.real>>num1.img;
    cout<<"Enter 2nd complex num.(real imaginary) ";
    cin>>num2.real>>num2.img;

    cout<<"Enter Your Choices\nEnter\n 1 to add\n 2 to subtract\n 3 to multiply\n 4 to divide.\n";


    cin>>x;

    switch(x)

    {

    case 1: add(num1,num2);
    break;
    case 2: subtract(num1,num2);
    break;
    case 3: multiply(num1,num2);
    break;
    case 4: divide(num1,num2);
    break;
    default : cout<<"wrong choice";


    }
    }

    void add(complex num1,complex num2)
    {
    complex num3;

    num3.real=num1.real+num2.real;
    num3.img=num1.img+num2.img;
    cout<<"\n\nThe result is : \n " ;
    if(num3.img>0)
    cout<0)
    cout<0)
    cout<0)
    cout<<num3.real<<" + "<<num3.img<<" i ";
    else
    cout<<num3.real<<" "<<num3.img<<" i ";
    }

    ReplyDelete

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.