-
Notifications
You must be signed in to change notification settings - Fork 0
/
BasicCalculator.java
81 lines (74 loc) · 2.68 KB
/
BasicCalculator.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
import java.util.Scanner;
public class BasicCalculator {
public static void main(String[] args) {
while (true) {
Scanner sc = new Scanner(System.in);
System.out.println("Press '1' : Addition");
System.out.println("Press '2' : Subtraction");
System.out.println("Press '3' : Multiplication");
System.out.println("Press '4' : Division");
System.out.println("Press '5' : Exit");
System.out.println("Enter option here");
int option = sc.nextInt();
options(option);
}
}
public static void options (int option){
switch (option) {
case 1: {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num1");
double num1 = sc.nextDouble();
System.out.println("Enter num2");
double num2 = sc.nextDouble();
System.out.println("Output is " + sum(num1, num2));
break;
}
case 2: {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num1");
double num1 = sc.nextDouble();
System.out.println("Enter num2");
double num2 = sc.nextDouble();
System.out.println("Output is " + sub(num1, num2));
break;
}
case 3: {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num1");
double num1 = sc.nextDouble();
System.out.println("Enter num2");
double num2 = sc.nextDouble();
System.out.println("Output is " + multiply(num1, num2));
break;
}
case 4: {
Scanner sc = new Scanner(System.in);
System.out.println("Enter num1");
double num1 = sc.nextDouble();
System.out.println("Enter num2");
double num2 = sc.nextDouble();
System.out.println("Output is " + divide(num1, num2));
break;
}
case 5:{
System.exit(0);
}
default: {
System.out.println("No option selected");
}
}
}
public static double sum ( double num1, double num2){
return (num1 + num2);
}
public static double multiply ( double num1, double num2){
return (num1 * num2);
}
public static double sub ( double num1, double num2){
return (num1 - num2);
}
public static double divide ( double num1, double num2){
return (num1 / num2);
}
}