0% found this document useful (0 votes)
47 views5 pages

Assignment No 3 New

Uploaded by

Krushna Deore
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)
47 views5 pages

Assignment No 3 New

Uploaded by

Krushna Deore
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/ 5

#include <iostream>

#include <cmath>

#include <stdexcept> // For handling division by zero

class Complex {

private:

double real, imag;

public:

// Constructor to initialize real and imaginary parts

Complex(double r = 0.0, double i = 0.0) : real(r), imag(i) {}

// Method to get the real part

double getReal() const { return real; }

// Method to get the imaginary part

double getImag() const { return imag; }

// Method to set the real part

void setReal(double r) { real = r; }

// Method to set the imaginary part

void setImag(double i) { imag = i; }

// String representation of the complex number

void display() const {

if (imag >= 0)

std::cout << real << " + " << imag << "i";

else
std::cout << real << " - " << -imag << "i";

// Addition of two complex numbers

Complex operator+(const Complex& other) const {

return Complex(real + other.real, imag + other.imag);

// Subtraction of two complex numbers

Complex operator-(const Complex& other) const {

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

// Multiplication of two complex numbers

Complex operator*(const Complex& other) const {

double realPart = real * other.real - imag * other.imag;

double imagPart = real * other.imag + imag * other.real;

return Complex(realPart, imagPart);

// Division of two complex numbers

Complex operator/(const Complex& other) const {

double denom = other.real * other.real + other.imag * other.imag;

if (denom == 0)

throw std::invalid_argument("Division by zero is not allowed");

double realPart = (real * other.real + imag * other.imag) / denom;

double imagPart = (imag * other.real - real * other.imag) / denom;

return Complex(realPart, imagPart);

}
// Magnitude (absolute value) of a complex number

double magnitude() const {

return sqrt(real * real + imag * imag);

// Conjugate of a complex number

Complex conjugate() const {

return Complex(real, -imag);

// Equality comparison

bool operator==(const Complex& other) const {

return real == other.real && imag == other.imag;

};

// Main function to test the Complex class

int main() {

Complex c1(3, 4); // 3 + 4i

Complex c2(1, 2); // 1 + 2i

std::cout << "Complex number c1: ";

c1.display();

std::cout << std::endl;

std::cout << "Complex number c2: ";

c2.display();

std::cout << std::endl;


// Addition

Complex sum = c1 + c2;

std::cout << "Sum of c1 and c2: ";

sum.display();

std::cout << std::endl;

// Subtraction

Complex diff = c1 - c2;

std::cout << "Difference of c1 and c2: ";

diff.display();

std::cout << std::endl;

// Multiplication

Complex product = c1 * c2;

std::cout << "Product of c1 and c2: ";

product.display();

std::cout << std::endl;

// Division

try {

Complex quotient = c1 / c2;

std::cout << "Quotient of c1 and c2: ";

quotient.display();

std::cout << std::endl;

} catch (const std::invalid_argument& e) {

std::cout << "Error: " << e.what() << std::endl;

}
// Magnitude of c1

std::cout << "Magnitude of c1: " << c1.magnitude() << std::endl;

// Conjugate of c1

Complex conj = c1.conjugate();

std::cout << "Conjugate of c1: ";

conj.display();

std::cout << std::endl;

// Equality check

std::cout << "c1 and c2 are " << (c1 == c2 ? "equal" : "not equal") << std::endl;

return 0;

Output

You might also like