Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

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)

 

 

 

Fazal Rehman Shamil Click Here to Know More
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325
Dear Professors and Resarchers!You are welome to Cite these tutorials in your research or slides etc. Please don't forget to mention the reference of website. Copy Paste of text is strcitly forbidden. Images can be reuse because images are protected with watermark.