-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
76 lines (49 loc) · 2.35 KB
/
Calculator.java
File metadata and controls
76 lines (49 loc) · 2.35 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
package school;
import java.util.Scanner;
/**
*
* @author rgarg
*/
public class Calculator {
public static void main(String[] args){
// Defining the variables
float num_1, num_2, add, diff, product, divide, remainder;
char command = 'a';
// Get the inputs
Scanner input = new Scanner(System.in);
do{
System.out.print("Enter the first number: ");
num_1 = input.nextInt();
System.out.print("Enter the second number: ");
num_2 = input.nextInt();
// Giving the selection choice
System.out.println("Enter 'a' for add, 's' for subtract, 'm' for multiply, 'd' for divide, 'r' for remainder, 'e' to exit");
command = input.next().charAt(0);
// Writing the if and else conditions
if (command == 'a'){
// Adding the 2 numbers
add = num_1 + num_2;
System.out.println("The sum of the two numbers is: "+ add);
}else if(command == 's'){
// subtracting the 2 numbers
diff = num_1 - num_2;
System.out.println("The difference of the 2 numbers is: "+ diff);
}else if(command == 'm'){
// multiplying the 2 numbers
product = num_1 * num_2;
System.out.println("The product of the 2 numbers is: " +product );
}else if(command == 'd'){
// diving the two numbers
divide = num_1/num_2;
System.out.println("The division of the numbers is: "+ divide);
}else if(command == 'r'){
//getting the remainder
remainder = num_1%num_2;
System.out.println("The remainder of the numbers is: " + remainder);
}else if(command == 'e'){
// Breaking the loop
break;
}
}while (true);
}
}