Skip to content

Commit f5a148f

Browse files
authored
A java program for basic calculator operations
1 parent 9fd3485 commit f5a148f

1 file changed

Lines changed: 38 additions & 0 deletions

File tree

Basic Calculator Operations.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#Subscribed to CodeHouseIndia
2+
import java.util.Scanner;
3+
public class Calculator {
4+
public static void main(String[] args) {
5+
Scanner reader = new Scanner(System.in);
6+
System.out.print("Enter two numbers: ");
7+
// nextDouble() reads the next double from the keyboard
8+
double first = reader.nextDouble();
9+
double second = reader.nextDouble();
10+
System.out.print("Enter an operator (+, -, *, /): ");
11+
char operator = reader.next().charAt(0);
12+
double result;
13+
//switch case for each of the operations
14+
switch(operator)
15+
{
16+
case '+':
17+
result = first + second;
18+
break;
19+
case '-':
20+
result = first - second;
21+
break;
22+
case '*':
23+
result = first * second;
24+
break;
25+
case '/':
26+
result = first / second;
27+
break;
28+
// operator doesn't match any case constant (+, -, *, /)
29+
30+
31+
default:
32+
System.out.printf("Error! operator is not correct");
33+
return;
34+
}
35+
//printing the result of the operations
36+
System.out.printf("%.1f %c %.1f = %.1f", first, operator, second, result);
37+
}
38+
}

0 commit comments

Comments
 (0)