-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScientificOperations.java
More file actions
103 lines (91 loc) · 2.66 KB
/
ScientificOperations.java
File metadata and controls
103 lines (91 loc) · 2.66 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
package sampleWorkout;
import java.util.Scanner;
import java.lang.Math;
public class ScientificOperations {
public void scientificOperations() {
Scanner sc = new Scanner(System.in);
int choice;
double num1, num2, result = 0;
while(true) {
System.out.println("Welcome to Scientific calculator menu!!! ");
System.out.println("Press: ");
System.out.println("1. Square root");
System.out.println("2. Power (a^b)");
System.out.println("3. Sine");
System.out.println("4. Cosine");
System.out.println("5. Tangent");
System.out.println("6. Exit");
choice = sc.nextInt();
switch(choice) {
case 1:
try {
System.out.print("Enter a number: ");
num1 = sc.nextDouble();
result = Math.sqrt(num1);
System.out.printf("√%.2f = $.4f\n", num1, result);
break;
}catch (Exception e) {
System.out.println(e.getMessage());
sc.nextLine();
break;
}
case 2:
try {
System.out.print("Enter base: ");
num1 = sc.nextDouble();
System.out.print("Enter exponent: ");
num2 = sc.nextDouble();
result = Math.pow(num1, num2);
System.out.printf("%.2f ^ %.2f = %.4f\n", num1, num2, result);
break;
} catch (Exception e) {
System.out.println(e.getMessage());
sc.nextLine();
break;
}
case 3:
try {
System.out.print("Enter the angle in degrees: ");
num1 = Math.toRadians(sc.nextDouble());
result = Math.sin(num1);
System.out.printf("cos = %.4f\n", result);
break;
} catch (Exception e) {
System.out.println(e.getMessage());
sc.nextLine();
break;
}
case 4:
try {
System.out.print("Enter angle in degrees: ");
num1 = Math.toRadians(sc.nextDouble());
result = Math.cos(num1);
System.out.printf("cos = %.4f\n", result);
break;
}catch (Exception e) {
System.out.println(e.getMessage());
sc.nextLine();
break;
}
case 5:
try {
System.out.print("Enter angle in degrees: ");
num1 = Math.toRadians(sc.nextDouble());
result = Math.tan(num1);
System.out.printf("tan = %.4f\n", result);
break;
} catch (Exception e) {
System.out.println(e.getMessage());
sc.nextLine();
break;
}
case 6:
System.out.println("Exiting Calculator. Goodbye!");
sc.close();
return;
default:
System.out.println("Invalid choice. Try again.");
}
}
}
}