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
S y m b o li c c o n s t a n t : c on s t m a x = 1 0 0 ; T h e d e f a u l t d a t a t y p e w i l l b e i n t e

Explain each of the algorithms in a way that would be understandable to an intelligent person who is not familiar with programming. You should not use any code (or even pseudo code

The digital signal is one which only consists of two states i.e. logic '1'   (+5 volts) and logic '0' (0 volts). Various electronic blocks use logic and these form the basis of a m

A: It is implementation dependent. Verify with your compiler's documentation. For instance, assume you wish to do binary I/O using std::cin & std::cout. Unluckily there is no st

A Padovan string P(n) for a natural number n is defined as: P(0) = ‘X’ P(1) = ‘Y’ P(2) = ‘Z’ P(n) = P(n-2) + P(n-3), n>2 where + denotes string concatenation. For a string of t

I need Computer application/program. Project Description:                                                                I want a project done. It is a computer desktop appli

Write a program to find the area under the curve y = f(x) between x = a and x = b, integrate y = f(x) between the limits of a and b. The area under a curve betw



A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome