Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions CalculatorProgram.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.Scanner;
public class CalculatorProgram {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
//Take input from from user till user press x or X
int ans = 0;
while(true) {
System.out.println("Enter the operator: ");
char op = sc.next().trim().charAt(0);
if(op == '+' || op == '-' || op == '*' || op == '/' || op == '%') {
//input two numbers
System.out.print("Input first num: ");
int num1 = sc.nextInt();
System.out.println("input 2nd num: ");
int num2 = sc.nextInt();

if(op == '+') {
ans = num1 + num2;
}
if(op == '-') {
ans = num1 - num2;
}
if(op == '*') {
ans = num1 * num2;
}
if(op == '/') {
if(num2 != 0) {
ans = num1 / num2;
}
}
if(op == '%') {
ans = num1 % num2;
}
else if(op == 'x' || op == 'X') {
break;
}else {
System.out.println("Invalid operation");
}
System.out.println(ans);
}
}

}
}