-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalculator.java
50 lines (38 loc) · 1.16 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
44
45
46
47
48
49
50
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 no1 = sc.nextInt();
System.out.print("Enter Second Number: ");
int no2 = sc.nextInt();
System.out.println("Enter Required Operation\n[| + | - | * | / | % |]");
char operator = sc.next().charAt(0);
int res = calculate(no1, no2, operator);
System.out.println(no1 + " " + operator + " " + no2 + " = " + res);
sc.close();
}
// Method to Perform Operations
static int calculate(int x, int y, char operator)
{
int res=0;
switch (operator)
{
case '+' : res=x+y;
break;
case '-' : res=x-y;
break;
case '*' : res=x*y;
break;
case '/' : res=x/y;
break;
case '%' : res=x%y;
break;
default : System.out.println("Invalid Key");
break;
}
return res;
}
}