0% found this document useful (0 votes)
33 views

C++ Complex Numbers by Passing Structure

The program allows the user to enter the real and imaginary parts of two complex numbers. It then calls a function to calculate the sum of the complex numbers and stores the result. The output displays the sum in standard complex number format with the correct sign for the imaginary part.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

C++ Complex Numbers by Passing Structure

The program allows the user to enter the real and imaginary parts of two complex numbers. It then calls a function to calculate the sum of the complex numbers and stores the result. The output displays the sum in standard complex number format with the correct sign for the imaginary part.

Uploaded by

Animal Cares
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

// Complex numbers are entered by the user

#include <iostream>
using namespace std;

typedef struct complex {


float real;
float imag;
} complexNumber;

complexNumber addComplexNumbers(complex, complex);

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

cout << "For 1st complex number," << endl;


cout << "Enter real and imaginary parts respectively:" << endl;
cin >> num1.real >> num1.imag;

cout << endl


<< "For 2nd complex number," << endl;
cout << "Enter real and imaginary parts respectively:" << endl;
cin >> num2.real >> num2.imag;

// Call add function and store result in complexSum


complexSum = addComplexNumbers(num1, num2);

// Use Ternary Operator to check the sign of the imaginary number


signOfImag = (complexSum.imag > 0) ? '+' : '-';

// Use Ternary Operator to adjust the sign of the imaginary number


complexSum.imag = (complexSum.imag > 0) ? complexSum.imag : -complexSum.imag;

cout << "Sum = " << complexSum.real << signOfImag << complexSum.imag << "i";

return 0;
}

complexNumber addComplexNumbers(complex num1, complex num2) {


complex temp;
temp.real = num1.real + num2.real;
temp.imag = num1.imag + num2.imag;
return (temp);
}

You might also like