Skip to content

Instantly share code, notes, and snippets.

@AnjaliManhas
Last active April 10, 2020 06:34
Show Gist options
  • Save AnjaliManhas/d47d05d3c557e948637b3da3451386cf to your computer and use it in GitHub Desktop.
Save AnjaliManhas/d47d05d3c557e948637b3da3451386cf to your computer and use it in GitHub Desktop.
Class Calculator calculates basic operations, namely, addition, subtraction, multiplication and division of two numbers.
package com.company;
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Calculator {
public static void main(String[] args) {
// write your code here
int a, b;
char opr;
char choice;
boolean n = true;
while (n = true) {
System.out.println("enter first number");
Scanner scan = new Scanner(System.in);
a = scan.nextInt();
System.out.println("enter second number");
b = scan.nextInt();
System.out.println("enter the operation you want to perform");
opr = scan.next().charAt(0);
switch (opr) {
case '+':
System.out.println(a + b);
break;
case '-':
System.out.println(a - b);
break;
case 'X':
System.out.println(a * b);
break;
case '/':
System.out.println(a / b);
break;
default:
System.out.println("operation is invalid");
break;
}
System.out.println("Do you want to continue? Y/N");
choice = scan.next().charAt(0);
if (choice == 'Y'|| choice== 'y')
continue;
if(choice== 'N' || choice== 'n')
break;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment