Program to Add Two Complex Numbers
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++
#include<bits/stdc++.h>
using namespace std;
class Complex {
public :
int real, imaginary;
Complex( int tempReal = 0, int tempImaginary = 0)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp;
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
};
int main()
{
Complex C1(3, 2);
cout<< "Complex number 1 : " << C1.real
<< " + i" << C1.imaginary<<endl;
Complex C2(9, 5);
cout<< "Complex number 2 : " << C2.real
<< " + i" << C2.imaginary<<endl;
Complex C3;
C3 = C3.addComp(C1, C2);
cout<< "Sum of complex number : "
<< C3.real << " + i"
<< C3.imaginary;
}
|
Java
import java.util.*;
class Complex {
int real, imaginary;
Complex() {}
Complex( int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
Complex addComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
}
public class GFG {
public static void main(String[] args)
{
Complex C1 = new Complex( 3 , 2 );
System.out.println( "Complex number 1 : " + C1.real
+ " + i" + C1.imaginary);
Complex C2 = new Complex( 9 , 5 );
System.out.println( "Complex number 2 : " + C2.real
+ " + i" + C2.imaginary);
Complex C3 = new Complex();
C3 = C3.addComp(C1, C2);
System.out.println( "Sum of complex number : "
+ C3.real + " + i"
+ C3.imaginary);
}
}
|
Python3
class Complex :
def __init__( self , tempReal, tempImaginary):
self .real = tempReal;
self .imaginary = tempImaginary;
def addComp( self , C1, C2):
temp = Complex ( 0 , 0 )
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
if __name__ = = '__main__' :
C1 = Complex ( 3 , 2 );
print ( "Complex number 1 :" , C1.real, "+ i" + str (C1.imaginary))
C2 = Complex ( 9 , 5 );
print ( "Complex number 2 :" , C2.real, "+ i" + str (C2.imaginary))
C3 = Complex ( 0 , 0 )
C3 = C3.addComp(C1, C2);
print ( "Sum of complex number :" , C3.real, "+ i" + str (C3.imaginary))
|
C#
using System;
public class Complex
{
public int real, imaginary;
public Complex()
{
}
public Complex( int tempReal, int tempImaginary)
{
real = tempReal;
imaginary = tempImaginary;
}
public Complex addComp(Complex C1, Complex C2)
{
Complex temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
}
public class GFG
{
public static void Main(String[] args)
{
Complex C1 = new Complex(3, 2);
Console.WriteLine( "Complex number 1 : "
+ C1.real + " + i"
+ C1.imaginary);
Complex C2 = new Complex(9, 5);
Console.WriteLine( "Complex number 2 : "
+ C2.real + " + i"
+ C2.imaginary);
Complex C3 = new Complex();
C3 = C3.addComp(C1, C2);
Console.WriteLine( "Sum of complex number : "
+ C3.real + " + i"
+ C3.imaginary);
}
}
|
Javascript
<script>
class Complex
{
constructor(tempReal, tempImaginary)
{
this .real = tempReal;
this .imaginary = tempImaginary;
}
}
function addComp(C1,C2)
{
let temp = new Complex();
temp.real = C1.real + C2.real;
temp.imaginary = C1.imaginary + C2.imaginary;
return temp;
}
let C1 = new Complex(3, 2);
document.write( "Complex number 1 : "
+ C1.real + " + i"
+ C1.imaginary+ "<br>" );
let C2 = new Complex(9, 5);
document.write( "Complex number 2 : "
+ C2.real + " + i"
+ C2.imaginary+ "<br>" );
let C3 = new Complex();
C3 = addComp(C1, C2);
document.write( "Sum of complex number : "
+ C3.real + " + i"
+ C3.imaginary+ "<br>" );
</script>
|
OutputComplex number 1 : 3 + i2
Complex number 2 : 9 + i5
Sum of complex number : 12 + i7
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...