diff --git a/Calculator.java b/Calculator.java new file mode 100644 index 0000000..0eb5d55 --- /dev/null +++ b/Calculator.java @@ -0,0 +1,41 @@ +import java.util.Scanner; + +public class Calculator { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + // take input from user till user does not press X or x + int ans = 0; + while (true) { + // take operator as a input + System.out.println("Enter the operator: "); + char op = in.next().trim().charAt(0); + + if (op == '+' || op == '-' || op == '*' || op == '/' || op == '%') { + // input two numbers + System.out.print("Enter two numbers: "); + int num1 = in.nextInt(); + int num2 = in.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; + } + + } + System.out.println("Ans: "+ ans); + } + } + } +}