Skip to content

Commit cc114bd

Browse files
committed
Added Practice Program.
Signed-off-by: Someshdiwan <[email protected]>
1 parent f17a9a6 commit cc114bd

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Arithmetic operation:
3+
4+
Write a C++ program that takes two numbers and an operator (+, -, *, /) as input.
5+
6+
Use a 'switch' statement to perform the corresponding arithmetic operation and print the result.
7+
8+
Check the sample input / output below for clarity.
9+
10+
(Note: It is guaranteed that in the hidden test cases, the second number will not be 0 during division.)
11+
*/
12+
13+
import java.util.Scanner;
14+
15+
public class SwitchCodeCC2 {
16+
public static void main(String[] args) {
17+
Scanner scanner = new Scanner(System.in);
18+
int num1 = scanner.nextInt();
19+
int num2 = scanner.nextInt();
20+
21+
//Take two integer as a input and choose a one operator's below this.
22+
char op = scanner.next().charAt(0);
23+
24+
switch (op) {
25+
case '+':
26+
System.out.println(num1 + num2);
27+
break;
28+
case '-':
29+
System.out.println(num1 - num2);
30+
break;
31+
case '*':
32+
System.out.println(num1 * num2);
33+
break;
34+
case '/':
35+
System.out.println(num1 / num2);
36+
break;
37+
default:
38+
System.out.println("Invalid operator");
39+
}
40+
}
41+
}

0 commit comments

Comments
 (0)