forked from Manish57-droid/python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComplex.java
More file actions
Latest commit
117 lines (92 loc) · 2.65 KB
/
Copy pathComplex.java
File metadata and controls
117 lines (92 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
importjava.util.*;
publicclassComplex {
floatreal;
floatimag;
Scannerscn = newScanner(System.in);
voidsetdata() {
System.out.println("enter the real value of complex number ");
real= scn.nextFloat();
System.out.println("enter the imaginary value of complex number ");
imag= scn.nextFloat();
}
voidadd(floata,floatb,floatc,floatd) {
real= a+c;
imag= b+d;
}
voidsubstract(floata,floatb,floatc,floatd) {
real= a-c;
imag= b-d;
}
voidmultiply(floata,floatb,floatc,floatd) {
real = (a*c)-(b*d);
imag = (a*d)+(b*c);
}
voiddivide(floata,floatb,floatc,floatd) {
real = ((a*c)+(b*d))/((c*c)+(d*d));
imag = ((b*c)-(a*d))/((c*c)+(d*d));
}
voidgetdata() {
if(imag>=0) {
System.out.println(real+"+"+imag+"i");
}
else {
System.out.println(real+""+imag+"i");
}
}
publicstaticvoidmain(String[] args) {
try (Scannerscn1 = newScanner(System.in)) {
Complexx2 = newComplex();
Complexx1= newComplex();
Complexaddition= newComplex();
Complexsubstraction = newComplex();
Complexdivision = newComplex();
Complexmultiplication = newComplex();
x1.setdata();
x2.setdata();
System.out.println("complex number1 is ");
x1.getdata();
System.out.println("complex number 2 is ");
x2.getdata();
intans=1;
while(ans==1) {
System.out.println("choose the operation to perform \n1.addtion\n2.substraction\n3.multiplication\n4.division");
inta= scn1.nextInt();
switch (a){
case1:
addition.add(x1.real,x1.imag,x2.real,x2.imag);
System.out.println("addition of complex1 and complex2 is ");
addition.getdata();
break;
case(2):
substraction.substract(x1.real,x1.imag,x2.real,x2.imag);
System.out.println("substraction of complex2 from complex1 is ");
substraction.getdata();
break;
case(3):
multiplication.multiply(x1.real,x1.imag,x2.real,x2.imag);
System.out.println("multiplication of comlex1 and complex2 is ");
multiplication.getdata();
break;
case(4):
division.divide(x1.real,x1.imag,x2.real,x2.imag);
if((x2.real==0)&&(x2.imag==0)) {
System.out.println("can't divide by zero");
}
else {
System.out.println("on division of complex1 by complex2, we get ");
division.getdata();
}
default:
System.out.println("Invalid option choosen!!!!");
}
System.out.println("do you want to check more?\n(press 1 for yes/2 for no)");
ans= scn1.nextInt();
if(ans==2) {
break;
}
}
}
//System.out.println("data type if marks is");
System.out.println("\nThank you");
}
}