C program to add two complex numbers , C/C++ Programming

Aim: To implement a program to add two complex numbers using constructors.

Code:                      

class complex

{

            int real;

            int img;

 

            public:

            complex(int r=0, int i=0);

            complex(complex c1, complex c2);

            void display();

};

 

complex :: complex(int r,int i)

{

 

            real=r;

            img=i;

}

complex :: complex(complex c1,complex c2)

{

            real=c1.real+c2.real;

            img=c1.img+c2.img;

}

void complex :: display()

{

            cout<

}

void main()

{

            int r1,r2,i1,i2;

            clrscr();

            cout<<"\nEnter first complex no:\nReal:";

            cin>>r1;

            cout<<"Imaginary:";

            cin>>i1;

            cout<<"\nEnter second complex no:\nReal:";

            cin>>r2;

            cout<<"Imaginary:";

            cin>>i2;

            complex c1(r1,i1);

            cout<<"\nFirst:\t\t";

            c1.display();

            cout<<"\nSecond:\t\t";

            complex c2(r2,i2);

            c2.display();

            complex c3(c1,c2);

            cout<<"\n----------------------\nAddition:\t";

            c3.display();

            getch();

}

Output:

Enter first complex no:

Real:25

Imaginary:3

Enter second complex no:

Real:12

Imaginary:71

 

First:          25+3i

Second:         12+71i

----------------------

Addition:       37+74i

Posted Date: 9/29/2012 4:00:07 AM | Location : United States







Related Discussions:- C program to add two complex numbers , Assignment Help, Ask Question on C program to add two complex numbers , Get Answer, Expert's Help, C program to add two complex numbers Discussions

Write discussion on C program to add two complex numbers
Your posts are moderated
Related Questions
Structures A structure is a user-defined data type, which may have different data types as its members. Creating a structure is a two-part process. First, a structure template


program to convert string from super ascii string with minimum cost

Integer literal Integer is numbers without fractional parts. e.g. 20       // Decimal 024      // Octal     0x14     // Hexadecimal To indicate long, unsigned,

Object Initialization An object of a derived class can be initialized to an object of a base class. If both the classes have similar data members, then no specific constructor

In which condition a template a better solution than a base class? A: While you are designing a generic class to contain or manage objects of other types, while the format & beh

Your task is to create a programming system for a ferry. The ferry transports passengers and vehicles (cars, busses, lorries and bicycles). The ferry has space for 200 passengers a

Converting Base Type to Class Type (char to string)   class String { char *name; int length; public: String(void) {length =0; name = new char[length+1];

SquareMaze The SquareMaze class should be declared and defined in maze.h and maze.cpp, respectively. Each SquareMaze object will represent a randomly-generated square maze and its

This question deals with AVL trees. The representation to be used is similar to the bank account object discussed in class. (a) Define a procedure called make-avl-tree which mak