C++ Program To Add Two Complex Numbers Last Updated : 23 Jun, 2023 Comments Improve Suggest changes Like Article Like Report Given two complex numbers of the form and the task is to add these two complex numbers. a1 + ib1 and a2 + ib2 Here 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: Sum = 9 + i15 Explanation: (4 + i8) + (5 + i7) = (4 + 5) + i(8 + 7) = 9 + i15 Below 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.The complexity of the above method Time Complexity: O(1) Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article C++ Program To Add Two Complex Numbers K kartik Follow Improve Article Tags : C++ Programs C++ C Basic Programs Practice Tags : CPP Similar Reads C++ program for Complex Number Calculator Pre-Requisites: Complex Numbers, Mathematics, Object-oriented Programming This is a Complex Number Calculator which performs a lot of operations which are mentioned below, to simplify our day-to-day problems in mathematics. The implementation of this calculator is done using C++ Programming Language 15+ min read C++ program to implement Full Adder Prerequisite : Full AdderWe are given three inputs of Full Adder A, B,C-IN. The task is to implement the Full Adder circuit and Print output i.e. sum and C-Out of three inputs. Introduction : A Full Adder is a combinational circuit that performs an addition operation on three 1-bit binary numbers. T 2 min read Add Two Numbers in C++ Given two integers, the task is to add these integer number and print their sum in C++.ExamplesInput: a = 11, b = 9Output: 20Explanation: Sum of 11 + 9 = 20Input: a = 1, b = 8Output: 9Explanation: Sum of 1 + 8 = 9 Add Two Numbers Using Addition OperatorIn C++, the simplest method for adding the two 3 min read How to add two Hexadecimal numbers? Given two numeric Hexadecimal numbers str1 and str2, the task is to add the two hexadecimal numbers. Hexadecimal Number system, often shortened to âhexâ, is a number system made up from 16 symbols. it uses 10 symbols from decimal number system which are represented by 0-9 and six extra symbols A - F 15+ min read C++ Program To Add Two Numbers Represented By Linked Lists- Set 1 Given two numbers represented by two lists, write a function that returns the sum list. The sum list is a list representation of the addition of two input numbers. Example: Input: List1: 5->6->3 // represents number 563 List2: 8->4->2 // represents number 842 Output: Resultant list: 1- 12 min read Like