C++ Challenge: Adding, Subtracting, and Multiplying Complex Numbers Simply
Objective:
C++ Challenge: Adding, Subtracting, and Multiplying Complex Numbers Simply
Write a C++ program to perform addition, subtraction, and multiplication on two
complex numbers without using loops, functions, or classes. You will handle the real
and imaginary parts of the complex numbers manually.
Problem Description:
A complex number is represented as a+bi where:
● a is the real part.
● a is the imaginary part.
For two complex numbers, represented as (a1 + b1i) and (a2 + b2i) perform the
following operations:
Addition:
Add the real parts and the imaginary parts separately.
Sum=(a1+a2)+(b1+b2)i
Subtraction:
Subtract the real parts and the imaginary parts separately.
Difference=(a1−a2)+(b1−b2)i
Multiplication:
Use the following formula to multiply the complex numbers:
Product=”create the logic on your own”
Task:
1. Declare four variables to store the real and imaginary parts of the two
complex numbers.
2. Take input for the real and imaginary parts of both complex numbers from the
user.
3. Calculate and display the results of:
○ Addition: Sum of the real parts and sum of the imaginary parts.
And print in this a + bi format
○ Subtraction: Difference between the real parts and difference between
the imaginary parts.
And print in this a + bi format
○ Multiplication: Calculate the real and imaginary parts of the product
using the formula provided.
Input Format:
● You will enter the real and imaginary parts of two complex numbers in the
following format:
○ For the first complex number: a1 b1
○ For the second complex number: a2 b2
Output Format:
● Display the results of the operations in the following format:
○ Result of addition: real + imaginary i
○ Result of subtraction: real - imaginary i
○ Result of multiplication: real + imaginary i
Solution
Code:
#include<iostream>
using namespace std;
int main() {
int real_1, imag_1, real_2, imag_2;
char option;
int sum_real, sum_img;
cout << "|||||| Complex Numbers Calculator ||||||" << endl;
cout << "--------------------------------------------" << endl;
cout << endl;
cout << "Enter Operator (+, -, *): " << endl;
cin >> option;
cout << "Enter Values" << endl;
cout << "---------------" << endl;
cout << "Enter 1st Real part: " << endl;
cin >> real_1;
cout << "Enter 1st Imaginary part: " << endl;
cin >> imag_1;
cout << "--------------------------------" << endl;
cout << "Enter 2nd Real part: " << endl;
cin >> real_2;
cout << "Enter 2nd Imaginary part: " << endl;
cin >> imag_2;
if (option == '+') {
sum_real = real_1 + real_2;
sum_img = imag_1 + imag_2;
cout << "Your Sum is: " << sum_real << (sum_img >= 0 ? "+" : "") << sum_img << "i" << endl;
} else if (option == '-') {
sum_real = real_1 - real_2;
sum_img = imag_1 - imag_2;
cout << "Your Subtraction is: " << sum_real << (sum_img >= 0 ? "+" : "") << sum_img << "i" << endl;
} else if (option == '*') {
// Corrected multiplication formula
sum_real = (real_1 * real_2) - (imag_1 * imag_2);
sum_img = (real_1 * imag_2) + (imag_1 * real_2);
cout << "Your Multiplication is: " << sum_real << (sum_img >= 0 ? "+" : "") << sum_img << "i" << endl;
} else {
cout << "Invalid operator!" << endl;
}
return 0;
}
Output
Thanks.......

Comments
Post a Comment