C++

Adding Complex Numbers in C++ - Detailed Instructions

This application displays the sum of the two complex numbers that the user enters.

Example: A user-entered program to add two complicated numbers

#include <iostream> 

using namespace std;

class complex_number
{
   public :
      int real, imag;
};

int main()
{
   complex_number num1, num2, sum;

   //getting the value of first complex number from user
   cout << "Enter real and imaginary parts of first complex number:"<<endl; 
   cin >> num1.real >> num1.imag;

   //getting the value of second complex number from user
   cout << "Enter real and imaginary parts of second complex number:"<<endl; 
   cin >> num2.real >> num2.imag;

   //addition of real and imaginary parts of complex numbers entered by user
   sum.real = num1.real + num2.real;
   sum.imag = num1.imag + num2.imag;

   //displaying the sum of complex numbers
   if ( sum.imag >= 0 )
      cout << "Sum of two complex numbers = " << sum.real << " + " << sum.imag << "i";
   else
      cout << "Sum of two complex numbers = " << sum.real << " - " << sum.imag << "i";

   return 0;
}

Output:

Enter real and imaginary parts of first complex number:
4 6
Enter real and imaginary parts of second complex number:
2 3
Sum of two complex numbers = 6 + 9i

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button