0% found this document useful (0 votes)
2 views3 pages

Expt 5 Printout

The document contains a C++ program that defines a Complex class for performing arithmetic operations on complex numbers. It includes methods for addition, subtraction, multiplication, and division, along with a display function to output the results. The main function demonstrates these operations with two complex numbers, displaying the results of each operation.

Uploaded by

dnyanesh.agale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Expt 5 Printout

The document contains a C++ program that defines a Complex class for performing arithmetic operations on complex numbers. It includes methods for addition, subtraction, multiplication, and division, along with a display function to output the results. The main function demonstrates these operations with two complex numbers, displaying the results of each operation.

Uploaded by

dnyanesh.agale
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EXPERIMENT NO: 05

Name : Agale Dnyanesh Nandkishor


Roll No. : 1

#include<iostream>

using namespace std;


class Complex {
private: float real, imag;
public:
Complex(float r = 0, float i = 0) {

real = r; imag = i;
}
Complex operator + (const Complex& obj) {
return Complex(real + obj.real, imag + obj.imag);
}
Complex operator - (const Complex& obj) {

return Complex(real - obj.real, imag - obj.imag);


}
Complex operator * (const Complex& obj) {
float r = (real * obj.real) - (imag * obj.imag);
float i = (real * obj.imag) + (imag * obj.real);
return Complex(r, i);
}
Complex operator / (const Complex& obj) {
float denominator = obj.real * obj.real + obj.imag * obj.imag;

float r = ((real * obj.real) + (imag * obj.imag)) / denominator;


float i = ((imag * obj.real) - (real * obj.imag)) / denominator;
return Complex(r, i);
}
void display() {
cout << real << (imag >= 0 ? " + " : " - ") << abs(imag) << "i" << endl;

}
};
int main() {
Complex c1(4, 5), c2(2, 3);
cout << "First Complex Number: ";

c1.display();
cout << "Second Complex Number: ";
c2.display();
Complex sum = c1 + c2;
cout << "\nAddition: ";
sum.display(); Complex diff = c1 - c2;

cout << "Subtraction: ";


diff.display();
Complex prod = c1 * c2;
cout << "Multiplication: ";
prod.display();
Complex div = c1 / c2;

cout << "Division: ";


div.display();
return 0;
}

OUTPUT:
First Complex Number: 4 + 5i
Second Complex Number: 2 + 3i

Addition : 6 + 8i
Subtraction: 2+ 2i
Multiplication: -7 + 22i
Division: 1.76923 – 0.153846i

You might also like