Write A Program To Perform Complex No. Operations
Source Code:
import java.io.*;
import java.lang.*;
class complex
{
double real,img;
complex c;
BufferedReader ob=new BufferedReader
(newInputStreamReader(System.in));
complex()
{
real=img=0;
}
void input(int x5)throws IOException
{
System.out.println("Enter real part of Number "+x5+":");
real=Double.parseDouble(ob.readLine());
System.out.println("Enter imaginary part of Number
"+x5+":");
img=Double.parseDouble(ob.readLine());
}
void display(complex c1,complex c2)
{
System.out.println("1st Number: ");
System.out.print(c1.real+"+i"+(c1.img));
System.out.println("\n2nd Number: ");
System.out.print(c2.real+"+i("+c2.img+")");
}
void add(complex c1,complex c2)
{
double x=c1.real+c2.real;
double y=c1.img+c2.img;
System.out.println("\nResult:"+x+"+i("+y+")");
}
void sub(complex c1,complex c2)
{
double x=c1.real-c2.real;
double y=c1.img-c2.img;
System.out.println("\nResult:"+x+"+i("+y+")");
}
void mult(complex c1,complex c2)
{
double x=(c1.real*c2.real)+(-1)*(c1.img*c2.img);
double y=(c1.real*c2.img)+(c1.img*c2.real);
System.out.println("\nResult:"+x+"+i("+y+")");
}
void div(complex c1,complex c2)
{
double x=((c1.real*c2.real)- (c1.img*c2.img))/((c2.real*c2.real)-(c2.img*c2.img));
double y=((c1.img*c2.real)-(c1.real*c2.img))/((c2.real*c2.real)-(c2.img*c2.img));
System.out.println("\nResult:"+x+"+i("+y+")");
}
void arg()
{
double x=Math.sqrt((real*real)+(img*img));
System.out.println("\nArgument="+x);
}
}
class complexmain
{
public static void main(String ars[])throws IOException
{
BufferedReader ob1=new BufferedReader(new InputStreamReader(System.in));
complex c=new complex();
complex c1=new complex();
complex c2=new complex();
for( ; ; )
{
System.out.println("\nEnter\n0 to take input\n1 to display\n2 to add\n3 to substract
\n4 to multiply\n5to divide\n6to get argument\n7 to exit");
System.out.println("\nEnter your choice:");
int x=Integer.parseInt(ob1.readLine());
switch (x)
{
case 0:
c1.input(1);
c2.input(2);
break;
case 1:
c.display(c1,c2);
break;
case 2:
c.add(c1,c2);
break;
case 3:
c.sub(c1,c2);
break;
case 4:
c.mult(c1,c2);
break;
case 5:
c.div(c1,c2);
break;
case 6:
System.out.println("\n1 for 1st comp no & 2 for 2nd comp no");
System.out.println("\nEnter your choice:");
int a=Integer.parseInt(ob1.readLine());
switch(a)
{
case 1:
c1.arg();
break;
case 2:
c2.arg();
break;
}
break;
case 7:
System.exit(0);
}
}
}
}
OUTPUT:
D:\java>javac assignment.java
D:\java>java complexmain
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
0
Enter real part of Number 1:
2
Enter imaginary part of Number 1:
3
Enter real part of Number 2:
4
Enter imaginary part of Number 2:
1
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
1
1st Number:
2.0+i3.0
2nd Number:
4.0+i(1.0)
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
2
Result:6.0+i(4.0)
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
3
Result:-2.0+i(2.0)
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
4
Result:5.0+i(14.0)
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
5
Result:0.3333333333333333+i(0.6666666666666666)
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
6
1 for 1st comp no & 2 for 2nd comp no
Enter your choice:
1
Argument=3.605551275463989
Enter
0 to take input
1 to display
2 to add
3 to substract
4 to multiply
5to divide
6to get argument
7 to exit
Enter your choice:
7
0 comments