Tuesday, September 19, 2017

C++ Code Review: Mathematics operations on two complex numbers

Description: This program shows how mathematics operation such as addition, subtraction, multiplication and division is carried out on two given complex numbers.

Code:

//Compiling Tool: Code::Block IDE with MinGW

//Program does maths operations on two complex numbers.

 

#include<iostream>

#include<math.h>

#include<conio.h> //for getch()

#include<cstdlib> //for system()

 

using namespace std;

 

struct complex

{

    float rel;

    float img;

} s1,s2;

 

int main()

{

    system("cls");//to clear the screen

    float a,b;

    cout<<"Enter real N imaginary part of 1st complex number:";

    cin>>s1.rel>>s1.img;

    cout<<"Enter real N imaginary part of 2nd complex number:";

    cin>>s2.rel>>s2.img;

 

//Addition

    a=(s1.rel)+(s2.rel);

    b=(s1.img)+(s2.img);

    cout<<"\nAddition:       "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

 

//Subtraction

    a=(s1.rel)-(s2.rel);

    b=(s1.img)-(s2.img);

    cout<<"\nSubtraction:    "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

 

//Multiplication

    a=((s1.rel)*(s2.rel))-((s1.img)*(s2.img));

    b=((s1.rel)*(s2.img))+((s2.rel)*(s1.img));

    cout<<"\nMultiplication: "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

 

//Division

    a=(((s1.rel)*(s2.rel))+((s1.img)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));

    b=(((s2.rel)*(s1.img))-((s1.rel)*(s2.img)))/(pow(s2.rel,2)+pow(s2.img,2));

    cout<<"\nDivision:       "<<"("<<a<<")"<<"+"<<"("<<b<<")"<<"i";

 

    getch();

    return(0);

}

Output:

clip_image002

If you want to write or construct or program C++ mini-project and do not know how or from where to start buy this simple e-book: Code Review of 26 C++ mini-projects. For book sample click this link.

Add your comments in below available comment box.

Note: Click these label/tags to view all related posts. Tags: C++Code

 

…till next post, bye-bye & take care.

No comments:

Post a Comment