BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

C++ Program to Add Complex Numbers

Last Updated: December 26, 2017 by Chaitanya Singh | Filed Under: C++ Programs

This program takes two complex numbers (entered by user) as input and displays the sum of them.

Example: Program to add two complex numbers entered by user

#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

Top Related Articles:

  1. C++ Program to display Armstrong Numbers between 1 and 1000
  2. C++ Program to add two numbers
  3. C++ Program to Find and display the Transpose of a Matrix
  4. C++ Program to find the sum of n natural numbers
  5. C++ Program to Convert Lowercase to Uppercase

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap