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
in this problem u given a board in which some of the elements are placed as shown in diagram below .each element represent a color .fill the other elements in the board such that n

Smugglers are becoming very smart day by day. Now they have developed a new technique of sending their messages from one smuggler to another. In their new technology, they are send

What happens if you write following code? string& foo()

Develop Pidgin for Windows Project Description: I want to build Pidgin (open source instant messenger) for Windows with D-Bus support enabled. I've effectively built both

Create an array of Strings and fill it with some interesting text, such as vacation destinations, album names, or weapon names from a CRPG. Display three elements of the array on t

Define Internal static storage class - computer programming? The Internal static variables are those that declared inside a function. The scope of the internal static variables

Convert compass heading in degrees (0-360 degrees)to compass bearing. A compass bearing consist of three items namely: a.The direction you face (North or South) b.An angle betwe

types of lists

C Program to check FACTORIAL   main() {             int i,j,m,fact,sum=0;           clrscr();           printf("ENTER THE NO.: ");           scanf("%d",&m);

Yes. "Memory pools" are useful in many situations. The bad news is that I'll need to drag you through the mire of how it acts before we talk about all the uses. Firstly, recall