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
#C Why don''t online compilers support the header file? What can I do to resolve this? PS. I have tried , does''nt work either. Thannk in advance.

Static Member Functions All the objects of the class share static data members of a class. The example above demonstrates how to keep track of all the objects of a class which

Operation on array: void Array::add( Object& toAdd ) {     lastElementIndex++;     while( ptrAt( lastElementIndex ) != ZERO &&            lastElementIndex


E x p r e s sion and their types: An expression will be in form of mathematical expression with C++ syntax embedded with it. Expressions are of following types which m

redirection in c++

Define a structure of student class:  Write a program to define a structure of student record in C. class student { char name[20]; int roll_no; int marks[2];

Program of swapping two varibales: void swap(int *, int *);   // This is swap's prototype int main() {                 int x = 5, y = 7;                 swap(&x, &

(a) Write a recursive procedure (digits n) that computes the number of digits in the integer n using a linear recursive process. For example, (digits 42) should return 2 and (digit

WAP TO PRINT SERIES FROM 1 TO 10 & FIND ITS SQUARE AND CUBE # include stdio.h> # include conio.h> # include math.h>   void main () { int a=1,sqr=0,cube=0;