-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCalculator.java
More file actions
52 lines (51 loc) · 1.39 KB
/
Copy pathCalculator.java
File metadata and controls
52 lines (51 loc) · 1.39 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
/*Qus 7: Write a simple calculator program that performs addition, subtraction, multiplication, or division based on user input.
Input:
The first line consists of two integers.
The second line consists of a character representing the operation (+, -, *, /).
Output:
Print the result of the operation.
Example:
Input:
8 4
*
Output:
32*/
import java.util.Scanner;
class Claculator
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
// Read the first line of input
int a = sc.nextInt();
int b = sc.nextInt();
// Read the second line of input
char operation = sc.next().charAt(0);
// Perform the operation
int result;
switch (operation) {
case '+':
result = a + b;
break;
case '-':
result = a - b;
break;
case '*':
result = a * b;
break;
case '/':
if (b != 0) {
result = a / b;
} else {
System.out.println("Error: Division by zero");
return;
}
break;
default:
System.out.println("Invalid operation");
return;
}
// Print the result
System.out.println(result);
}
}