Wednesday, 27 August 2014

Filled Under: ,

C++ program to add two complex numbers

Share
C++ program to add two complex numbers:Complex number means the combination of real number and imaginary number, which is in the form of  a + ib, where a is the real number and ib is imaginary number.This program accept the real and imaginary parts of two complex number and add them.Sum of two complex number is also a complex number.


C++ program to add two complex numbers

C++ program to add two complex numbers

#include <iostream.h>
 

class complex
{
   public :
      int real, img;
};
 
int main()
{
   complex a, b, c;
 
   cout << "Enter a and b where a + ib is the first complex number.";
   cout << "\na = ";
   cin >> a.real;
   cout << "b = ";
   cin >> a.img;
   cout << "Enter c and d where c + id is the second complex number.";
   cout << "\nc = ";
   cin >> b.real;
   cout << "d = ";
   cin >> b.img;
 
   c.real = a.real + b.real;
   c.img = a.img + b.img;
 
   if ( c.img >= 0 )
      cout << "Sum of two complex numbers = " << c.real << " + " << c.img << "i";
   else
      cout << "Sum of two complex numbers = " << c.real << " " << c.img << "i";
 
   return 0;
}

Output

C++ program to add two complex numbers