-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
43 lines (33 loc) · 1.22 KB
/
Calculator.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
/*
Problem Statement:
------------------
Write a program that accepts two numbers and a operator like (+,-,*, /) as command
line arguments and perform the appropriate operation indicated by the operator. If the
user enters any other character the appropriate message will be displayed. The output of
the program should be displayed to the user.
*/
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter first number: ");
int num1 = sc.nextInt();
System.out.print("Enter second number: ");
int num2 = sc.nextInt();
System.out.print("Enter operator : ");
char operator = sc.next().charAt(0);
try {
if (operator == '+')
System.out.println(num1 + num2);
else if (operator == '-')
System.out.println(num1 - num2);
else if (operator == '*')
System.out.println(num1 * num2);
else if (operator == '/')
System.out.println(num1 / num2);
} catch (NumberFormatException e) {
System.out.println("Something went wrong.");
}
sc.close();
}
}