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

Cpp Program 1

The document contains a C++ program that defines a Complex class for handling complex numbers. It includes methods for setting values, displaying values, and performing arithmetic operations such as addition, subtraction, multiplication, and scalar multiplication. The main function demonstrates the usage of these methods with two complex number instances.

Uploaded by

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

Cpp Program 1

The document contains a C++ program that defines a Complex class for handling complex numbers. It includes methods for setting values, displaying values, and performing arithmetic operations such as addition, subtraction, multiplication, and scalar multiplication. The main function demonstrates the usage of these methods with two complex number instances.

Uploaded by

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

#include <iostream>

using namespace std;

class Complex {

private:

float real;

float imag;

public:

// Constructor to initialize complex number

Complex(float r = 0, float i = 0) : real(r), imag(i) {}

// Function to set the value of the complex number

void setValue(float r, float i) {

real = r;

imag = i;

// Function to show the value of the complex number

void showValue() const {

cout << "Complex Number: " << real << " + " << imag << "i" << endl;

// Function to add two complex numbers

Complex add(const Complex& c) const {

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

// Function to subtract two complex numbers

Complex subtract(const Complex& c) const {

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


}

// Function to multiply two complex numbers

Complex multiply(const Complex& c) const {

float r = real * c.real - imag * c.imag;

float i = real * c.imag + imag * c.real;

return Complex(r, i);

// Function to multiply the complex number with a scalar value

Complex multiplyScalar(float scalar) const {

return Complex(real * scalar, imag * scalar);

};

int main() {

Complex c1(3, 4), c2(1, 2);

// Showing the values of the complex numbers

cout << "Complex number c1: ";

c1.showValue();

cout << "Complex number c2: ";

c2.showValue();

// Adding complex numbers

Complex resultAdd = c1.add(c2);

cout << "Addition result: ";

resultAdd.showValue();

// Subtracting complex numbers

Complex resultSub = c1.subtract(c2);


cout << "Subtraction result: ";

resultSub.showValue();

// Multiplying complex numbers

Complex resultMul = c1.multiply(c2);

cout << "Multiplication result: ";

resultMul.showValue();

// Multiplying complex number with a scalar

float scalar = 2;

Complex resultScalar = c1.multiplyScalar(scalar);

cout << "Multiplying c1 by scalar " << scalar << ": ";

resultScalar.showValue();

return 0;

}`

You might also like