C++ Program to Add Complex Numbers by Passing Structure to a Function

In this program, two complex numbers entered by the user are stored in the structures num1 and num2.

  • These two structures are passed to addComplexNumbers() function which calculates the sum and returns the result to the main() function.
  • This result is stored in the structure complexSum.
  • Then, the sign of the imaginary part of the sum is determined and stored in the char variable signOfImag.
  • If the imaginary part of complexSum is positive, then signofimag is assigned the value '+'. Else, it is assigned the value '-'
  • We then adjust the value of complexSum.imag.
  • This code changes complexSum.imag to positive if it is found to be of negative value.
  • This is because if it is negative, then printing it along with signofimag will give us two negative signs in the output.
  • So, we change the value to positive to avoid sign repetition.
  • After this we finally display the sum.
[mai mult...]