C++ Program To Add Two Complex Numbers Last Updated : 07 Aug, 2025 Comments Improve Suggest changes Like Article Like Report Given two complex numbers of the form and we have to add these two complex numbers.a1 + ib1 and a2 + ib2Here the values of real and imaginary numbers are passed while calling the parameterized constructor and, with the help of a default(empty) constructor, the function addComp is called to get the addition of complex numbers.Examples:Input: a1 = 4, b1 = 8 a2 = 5, b2 = 7 Output: 9 + i15 Explanation: Adding real part of both number together and same for the imaginary part = (4 + 5) + i(8 + 7) = 9 + i15Below is the C++ program to add two complex numbers: C++ // C++ Program to Add // Two Complex Numbers // Importing all libraries #include<bits/stdc++.h> using namespace std; // User Defined Complex class class Complex { // Declaring variables public: int real, imaginary; // Constructor to accept // real and imaginary part Complex(int tempReal = 0, int tempImaginary = 0) { real = tempReal; imaginary = tempImaginary; } // Defining addComp() method // for adding two complex number Complex addComp(Complex C1, Complex C2) { // Creating temporary variable Complex temp; // Adding real part of // complex numbers temp.real = C1.real + C2.real; // Adding Imaginary part of // complex numbers temp.imaginary = (C1.imaginary + C2.imaginary); // Returning the sum return temp; } }; // Driver code int main() { // First Complex number Complex C1(3, 2); // printing first complex number cout << "Complex number 1 : " << C1.real << " + i" << C1.imaginary << endl; // Second Complex number Complex C2(9, 5); // Printing second complex number cout << "Complex number 2 : " << C2.real << " + i" << C2.imaginary << endl; // For Storing the sum Complex C3; // Calling addComp() method C3 = C3.addComp(C1, C2); // Printing the sum cout << "Sum of complex number : " << C3.real << " + i" << C3.imaginary; } OutputComplex number 1 : 3 + i2 Complex number 2 : 9 + i5 Sum of complex number : 12 + i7Explanation of the above method A class Complex is created for complex numbers with two data members real and imaginary, a parameterized constructor, and a function to add complex numbers. Parameterized constructor is used to initialize the data members real and imaginary. A function addComp() with Class return type is created to add two complex numbers. Complex Number Addition Using Structure Visit Course Comment More infoAdvertise with us K kartik Follow Improve Article Tags : C++ Programs C++ C Basic Programs Explore Introduction to C++Introduction to C++ Programming Language2 min readHeader Files in C++5 min readSetting up C++ Development Environment8 min readDifference between C and C++3 min readBasicsC++ Data Types7 min readC++ Variables4 min readOperators in C++9 min readBasic Input / Output in C++5 min readControl flow statements in Programming15+ min readC++ Loops7 min readFunctions in C++8 min readC++ Arrays8 min readStrings in C++5 min readCore ConceptsPointers and References in C++5 min readnew and delete Operators in C++ For Dynamic Memory5 min readTemplates in C++8 min readStructures, Unions and Enumerations in C++3 min readException Handling in C++11 min readFile Handling through C++ Classes8 min readMultithreading in C++8 min readNamespace in C++5 min readC++ OOPInheritance in C++10 min readC++ Polymorphism5 min readEncapsulation in C++4 min readAbstraction in C++4 min readStandard Template Library(STL)Containers in C++ STL3 min readIterators in C++ STL10 min readC++ STL Algorithm Library2 min readPractice C++C++ Interview Questions and Answers1 min readTop C++ DSA Related ProblemsC++ Programming Examples7 min read Like