Saturday, 25 February 2012

Addition/Multiplication of complex numbers using structures

<posted by moeed>
#include<iostream>
using namespace std;

struct calc
{
int num1;
int num2;
};

calc real;
calc img;

int main()
{
//int num;
cout<<"Enter the real part of num1 : ";
cin>>real.num1;
cout<<"Enter the imaginary part of num1 : ";
cin>>img.num1;
cout<<"You entered : "<<real.num1<<" + "<<img.num1<<"i"<<endl;
cout<<"Enter the real part of num2 : ";
cin>>real.num2;
cout<<"Enter the real part of num2 : ";
cin>>img.num2;
cout<<"You entered : "<<real.num2<<" + "<<img.num2<<"i"<<endl;
//addition
int real_part = real.num1 + real.num2;
int img_part = img.num1 + img.num2 ;

cout<<"The Result (Addition): "<<real_part<<" + "<<img_part<<"i"<<endl;
//multiplication
int reall_2=img.num1 * img.num2;
reall_2=reall_2 * (-1);
int real_part_multi = real.num1 * real.num2 + reall_2;
int img_part_multi = real.num1 * img.num2 + img.num1 * real.num2 ;

cout<<"The Result (Multiplication): "<<real_part_multi<<" + "<<img_part_multi<<"i"<<endl;
return 0;
}

No comments:

Post a Comment