-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinput_def.java
More file actions
82 lines (68 loc) · 2.49 KB
/
Copy pathinput_def.java
File metadata and controls
82 lines (68 loc) · 2.49 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
// Make calculator using function:
import java.util.Scanner;
public class input_def {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter the first number: ");
int num1 = input.nextInt();
System.out.print("Enter the second number: ");
int num2 = input.nextInt();
System.out.println("\nChoose an operation:\n");
System.out.println("1. Addition (+)");
System.out.println("2. Subtraction (-)");
System.out.println("3. Multiplication (*)");
System.out.println("4. Division (/)");
System.out.println("5. Modulo (%)");
System.out.print("\nEnter your choice (1-5):");
int choice = input.nextInt();
int total_sum = 0;
boolean isValid = true;
switch (choice) {
case 1 -> {
total_sum = sum(num1, num2);
System.out.println("The given number of Addition is: " + total_sum);
}
case 2 -> {
total_sum = subtract(num1, num2);
System.out.println("The given number of Subtraction is: " + total_sum);
}
case 3 -> {
total_sum = multiply(num1, num2);
System.out.println("The given number of Multiplication is: " + total_sum);
}
case 4 -> {
if (num2 != 0) {
double divide = divide(num1, num2);
System.out.println("The given number of Division is: " + divide);
} else {
System.out.println("Error! Division by zero is not allowed.");
}
}
case 5 -> {
if (num2 != 0) {
total_sum = modulo(num1, num2);
System.out.println("The given number of Modulo is: " + total_sum);
} else {
System.out.println("Error! Modulo by zero is not allowed.");
}
}
default -> System.out.println("Invalid choice! Please enter a number between 1 and 5.");
}
input.close();
}
static int sum(int a, int b) {
return a + b;
}
static int subtract(int a, int b) {
return a - b;
}
static int multiply(int a, int b) {
return a * b;
}
static double divide(int a, int b) {
return (double) a / b;
}
static int modulo(int a, int b) {
return a % b;
}
}