Q.
1:complex number program in c++
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
class complex
public:
float r,i;
complex()
r==0.0;
i==0.0;
complex(float a,float b)
r=a;
i=b;
friend void operator<<(ostream &a,complex &b)
a<<"\n"<<b.r<<"+"<<b.i<<"i";
friend istream & operator>>(istream &input,complex &num)
{
input>>num.r>>num.i;
return input;
complex operator+(complex);
complex operator*(complex);
};
complex complex :: operator+(complex A)
complex B;
B.r=r+A.r;
B.i=i+A.i;
return B;
complex complex :: operator*(complex A)
complex B;
B.r=r+A.r;
B.i=i-A.i;
B.r=((r)*(A.r))-((i)*(A.i));
B.i=((r)*(A.i))+((A.r)*(i));
return B;
int main()
{
int ch;
complex A,B,C;
cout<<"\nOperations on complex numbers";
while(1)
cout<<"\n*************Menu*******************";
cout<<"\n1.Create constructor.";
cout<<"\n2.Accept nos.";
cout<<"\n3.Display nos.";
cout<<"\n4.Add nos.";
cout<<"\n5.Multiplication of numbers.";
cout<<"\n6.Exit";
cout<<"\nEnter the choice:";
cin>>ch;
switch(ch)
case 1:
cout<<"\n complex nos.after initialization:";
cout<<A;
cout<<B;
break;
case 2:
cout<<"\n Enter the real and imaginary part of 1 st number:";
cin>>A;
cout<<"\n Enter the real and imaginary part of 2nd nos:";
cin>>B;
break;
case 3:
cout<<"\n 1st complex number:"<<A;
cout<<"\n 2nd complex number:"<<B;
break;
case 4:
C=A+B;
cout<<"\n Addition is:"<<C;
break;
case 5:
C=A*B;
cout<<"\n Multiplication is:"<<C;
break;
case 6:
exit(0);
return 0;
OUTPUT
/tmp/eyRe8iD5vC.o
Operations on complex numbers
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:1
complex nos.after initialization:
0+0i
2.75813e-10+3.09267e-41i
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:2
Enter the real and imaginary part of 1 st number:20
30
Enter the real and imaginary part of 2nd nos:3
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:3
1st complex number:
20+30i
2nd complex number:
3+5i
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:4
Addition is:
23+35i
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:5
Multiplication is:
-90+190i
*************Menu*******************
1.Create constructor.
2.Accept nos.
3.Display nos.
4.Add nos.
5.Multiplication of numbers.
6.Exit
Enter the choice:6