Program to Add Two Complex Numbers - GeeksforGeeks

18 Feb, 2022
Given two complex numbers of the form and the task is to add these two complex numbers.
and 
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.
Illustration:  
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

Input:  a1 = 9, b1 = 3
        a2 = 6, b2 = 1
Output: 15 + i4 
The following program is an illustration of the above example.
C++
Java
Python3
C#
Javascript
//  C++ Program to Add Two Complex Numbers // Importing all libraries#include<bits/stdc++.h>using namespace std;  // User Defined Complex classclass 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;    }};  // Main Classint 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;} // This code is contributed by chitranayal
// Java Program to Add Two Complex Numbers // Importing all utility classesimport java.util.*; // Class 1// Helper class// User Defined Complex classclass Complex {     // Declaring variables    int real, imaginary;     // Constructors of this class     // Constructor 1 - Empty Constructor    Complex() {}     // Constructor 2     // Parameterised constructor    // Constructor to accept    // real and imaginary part    Complex(int tempReal, int tempImaginary)    {        real = tempReal;        imaginary = tempImaginary;    }     // Method    // To add two complex number    Complex addComp(Complex C1, Complex C2)    {        // Creating temporary variable        Complex temp = new Complex();         // 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;    }} // Class 2// Main Classpublic class GFG {     // Main function    public static void main(String[] args)    {         // First Complex number        Complex C1 = new Complex(3, 2);         // printing first complex number        System.out.println("Complex number 1 : " + C1.real                           + " + i" + C1.imaginary);         // Second Complex number        Complex C2 = new Complex(9, 5);         // Printing second complex number        System.out.println("Complex number 2 : " + C2.real                           + " + i" + C2.imaginary);         // Storing the sum        Complex C3 = new Complex();         // Calling addComp() method        C3 = C3.addComp(C1, C2);         // Printing the sum        System.out.println("Sum of complex number : "                           + C3.real + " + i"                           + C3.imaginary);    }}
# Python3 program to Add two complex numbers  # User Defined Complex classclass Complex:     # Constructor to accept    # real and imaginary part    def __init__(self, tempReal, tempImaginary):          self.real = tempReal;        self.imaginary = tempImaginary;     # Defining addComp() method    # for adding two complex number    def addComp(self, C1, C2):         # creating temporary variable        temp=Complex(0, 0)          # 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 codeif __name__=='__main__':     # First Complex number    C1 = Complex(3, 2);     # printing first complex number    print("Complex number 1 :", C1.real, "+ i" + str(C1.imaginary))     # Second Complex number    C2 = Complex(9, 5);     # printing second complex number    print("Complex number 2 :", C2.real, "+ i" + str(C2.imaginary))          # for Storing the sum    C3 = Complex(0, 0)     # calling addComp() method    C3 = C3.addComp(C1, C2);     # printing the sum    print("Sum of complex number :", C3.real, "+ i"+ str(C3.imaginary)) # This code is contributed by rutvik_56.
// C# program to Add two complex numbersusing System; // User Defined Complex classpublic class Complex{     // Declaring variables    public int real, imaginary;     // Empty Constructor    public Complex()    {    }     // Constructor to accept    // real and imaginary part    public Complex(int tempReal, int tempImaginary)    {        real = tempReal;        imaginary = tempImaginary;    }     // Defining addComp() method    // for adding two complex number    public Complex addComp(Complex C1, Complex C2)    {        // creating temporary variable        Complex temp = new Complex();         // 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;    }} // Main Classpublic class GFG{     // Main function    public static void Main(String[] args)    {         // First Complex number        Complex C1 = new Complex(3, 2);         // printing first complex number        Console.WriteLine("Complex number 1 : "                        + C1.real + " + i"                        + C1.imaginary);         // Second Complex number        Complex C2 = new Complex(9, 5);         // printing second complex number        Console.WriteLine("Complex number 2 : "                        + C2.real + " + i"                        + C2.imaginary);         // for Storing the sum        Complex C3 = new Complex();         // calling addComp() method        C3 = C3.addComp(C1, C2);         // printing the sum        Console.WriteLine("Sum of complex number : "                        + C3.real + " + i"                        + C3.imaginary);    }} // This code is contributed by Princi Singh
<script>// Javascript program to Add two complex numbers         // User Defined Complex class    class Complex    {        // Constructor to accept        // real and imaginary part        constructor(tempReal, tempImaginary)        {            this.real = tempReal;            this.imaginary = tempImaginary;           }    }         // Defining addComp() method    // for adding two complex number    function addComp(C1,C2)    {        // creating temporary variable        let temp = new Complex();          // 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;    }         // First Complex number    let C1 = new Complex(3, 2);      // printing first complex number    document.write("Complex number 1 : "                           + C1.real + " + i"                           + C1.imaginary+"<br>");      // Second Complex number    let C2 = new Complex(9, 5);      // printing second complex number    document.write("Complex number 2 : "                           + C2.real + " + i"                           + C2.imaginary+"<br>");      // for Storing the sum    let C3 = new Complex();      // calling addComp() method    C3 = addComp(C1, C2);      // printing the sum    document.write("Sum of complex number : "                           + C3.real + " + i"                           + C3.imaginary+"<br>");      // This code is contributed by avanitrachhadiya2155</script>
Complex number 1 : 3 + i2
Complex number 2 : 9 + i5
Sum of complex number : 12 + i7
princi singh
ukasp
khushboogoyal499
rutvik_56
avanitrachhadiya2155
utkarshgupta04092003
simranarora5sos
number-theory
Java Programs
School Programming
Technical Scripter
number-theory
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Java Programming Examples
How to Iterate HashMap in Java?
Factory method design pattern in Java
Traverse Through a HashMap in Java
Iterate through List in Java
Python Dictionary
Arrays in C/C++
Reverse a string in Java
Inheritance in C++
C++ Classes and Objects