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

Source Code: Write A Program To Add, Subtract and Multiply Two Complex Numbers Using Structures To Function

The document describes a C++ program that uses a structure to define complex numbers with real and imaginary parts. It includes functions to add, subtract, and multiply two complex numbers by taking the real and imaginary parts of each number as arguments. The main function gets real and imaginary inputs from the user for two complex numbers and calls the add, subtract, and multiply functions to perform the operations and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
66 views

Source Code: Write A Program To Add, Subtract and Multiply Two Complex Numbers Using Structures To Function

The document describes a C++ program that uses a structure to define complex numbers with real and imaginary parts. It includes functions to add, subtract, and multiply two complex numbers by taking the real and imaginary parts of each number as arguments. The main function gets real and imaginary inputs from the user for two complex numbers and calls the add, subtract, and multiply functions to perform the operations and output the results.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Task 5

Write a program to add, subtract and multiply two complex numbers using structures to
function.
Source code
#include<iostream>
#include<math.h>
using namespace std;

struct complex
{
int real;
int img;

void add(complex a, complex b)


{
complex temp;
temp.real = a.real + b.real;
temp.img = a.img + b.img;
cout << "Sum=" << endl << temp.real << " + " << temp.img << "i" <<
endl;
}

void subtract(complex a, complex b)


{
complex temp;
if (a.real > b.real)
{
temp.real = a.real - b.real;
}
if (a.real < b.real)
{
temp.real = b.real-a.real;
}
if (a.img > b.img)
{
temp.img = a.img- b.img;
}
if (a.img < b.img)
{
temp.img = b.img - a.img;
}
if (a.real > b.real && a.img > b.img)
{
cout << "Subtraction=" << endl << temp.real << " + " <<
temp.img << "i" << endl;
}
if (a.real > b.real && a.img < b.img)
{
cout << "Subtraction=" << endl << temp.real << " - " <<
temp.img << "i" << endl;
}
if (a.real < b.real && a.img > b.img)
{
cout << "Subtraction=" << endl <<"-" <<temp.real << " + " <<
temp.img << "i" << endl;
}
if (a.real < b.real && a.img < b.img)
{
cout << "Subtraction=" << endl << "-" << temp.real << " - " <<
temp.img << "i" << endl;
}
}
void multiply(complex a,complex b)
{
complex temp;
temp.real = (a.real*b.real) - (a.img*b.img);
temp.img = (a.real*b.real) + (a.img*b.img);
cout << "Multiplication=" << endl <<temp.real << " + " << temp.img
<< "i" << endl;
}
};

int main()
{
complex obj,obj1,obj2;
cout << "Enter First Number" << endl;
cout << "real Part" << endl;
cin >> obj1.real;
cout << "Imaginary part" << endl;
cin >> obj1.img;
cout << "Enter Second Number" << endl;
cout << "real Part" << endl;
cin >> obj2.real;
cout << "Imaginary part" << endl;
cin >> obj2.img;
obj.add(obj1,obj2);
obj.subtract(obj1, obj2);
obj.multiply(obj1, obj2);

system("pause");
return 0;
}

You might also like