Program for the addition of two complex numbers in CPP (C plus plus)

Program for the addition of two complex numbers in CPP (C plus plus)

In this tutorial, we will learn about Program for the addition of two complex numbers in CPP (C plus plus).

#include <iostream>

using namespace std;

typedef struct complex
{
    float real;
    float imaginary;
}
 complexNumber;

complexNumber addComplexNumbers(complex, complex);

int main()
{
    complexNumber num1, num2, temporaryNumber;
    char signOfImag;

    cout<<"1st complex number: "<< endl;
    cout<<"Please enter real & imaginary parts respectively: "<<endl;
    cin>>num1.real >> num1.imaginary;

    cout<<endl<<"2nd complex number: "<<endl;
    cout<<"Please enter real and imaginary parts respectively:" <<endl;
    cin>>num2.real>>num2.imaginary;

    signOfImag = (temporaryNumber.imaginary > 0) ? '+' : '-';
    temporaryNumber.imaginary = (temporaryNumber.imaginary > 0) ? temporaryNumber.imaginary : -temporaryNumber.imaginary; 

    temporaryNumber = addComplexNumbers(num1, num2);    
    cout<<"Sum = "<< temporaryNumber.real<<"  "<<temporaryNumber.imaginary<<"i";
    return 0;
}

complexNumber addComplexNumbers(complex num1,complex num2)
{
      complex tmp;
      tmp.real = num1.real+num2.real;
      tmp.imaginary = num1.imaginary+num2.imaginary;
      return(tmp);
}

Output:

Program for the addition of two complex numbers in CPP (C plus plus)
Program for the addition of two complex numbers in CPP (C plus plus)

 

 

 

Prof.Fazal Rehman Shamil (Available for Professional Discussions)
1. Message on Facebook page for discussions,
2. Video lectures on Youtube
3. Email is only for Advertisement/business enquiries.