forked from PrajaktaSathe/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathComplexNosOps.java
102 lines (99 loc) · 2.91 KB
/
ComplexNosOps.java
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
// Program to demonstrate operations on complex numbers in Java -
package ComplexNumsOps;
import java.util.Scanner; // For user-input
class Complex {
double real1, real2, imag1, imag2;
// Function to input data from user
public void get_data() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter real part of first complex number: ");
real1 = sc.nextDouble();
System.out.println("Enter imaginary part first of complex number: ");
imag1 = sc.nextDouble();
System.out.println("Enter real part of second complex number: ");
real2 = sc.nextDouble();
System.out.println("Enter imaginary part second of complex number: ");
imag2 = sc.nextDouble();
}
// Function to display data that has been taken as input from user
public void display() {
System.out.println("Entered first complex number is: " + real1 + " + " + imag1 + "i");
System.out.println("Entered second complex number is: " + real2 + " + " + imag2 + "i");
}
// Function to add two complex numbers
public void add() {
double addr = real1 + real2;
double addi = imag1 + imag2;
System.out.println("Addition is: " + addr + " + " + addi + "i");
}
// Function to subtract two complex numbers
public void sub() {
double subr = real1 - real2;
double subi = imag1 - imag2;
System.out.println("Subtraction is: " + subr + " + " + subi + "i");
}
// Function to multiply two complex numbers
public void mult() {
double s1, s2, s3, rs2;
s1 = real1 * real2;
s2 = (real1 * imag2) + (real2 * imag1);
rs2 = Math.round((s2 * 100)/100);
if (imag1 == 0 || imag2 == 0) {
s3 = 0;
}
else {
s3 = (-1) * (imag1 * imag2);
}
s1 = s1 + s3;
System.out.println("Multiplication is: " + s1 + " + " + rs2 + "i");
}
// Function to divide two complex numbers
public void div() {
double d1, d2, d3, d4, d5, d6;
if (real2 == 0 && imag2 == 0) {
System.out.println("Division cannot be performed.");
}
else {
d1 = real1 * real2; // For numerator
d2 = (real1 * imag2) + (real2 * imag1); // For numerator
d4 = (real2 * real2); // For denominator
d5 = (real2 * (-1) * imag2) + (real2 * (-1) * imag2); // For denominator
d6 = d4 + d5; // For denominator
if (imag1 == 0 || imag2 == 0) {
d3 = 0;
}
else {
d3 = (-1) * (imag1 * imag2);
}
d1 = d1 + d3;
System.out.println("Division is: " + d1/d6 + " + " + d2/d6 + "i");
}
}
};
public class CmplxNumOps {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int choice = 0;
Complex c1 = new Complex(); // Creation of object of class Complex
c1.get_data();
c1.display();
System.out.println("Enter 1 for addition, 2 for subtraction, 3 for multiplication, 4 for division: ");
choice = sc.nextInt();
switch (choice) {
case 1:
c1.add();
break;
case 2:
c1.sub();
break;
case 3:
c1.mult();
break;
case 4:
c1.div();
break;
default:
System.out.println("Wrong input. Please enter correct choice.");
}
}
}