Skip to content

Instantly share code, notes, and snippets.

@singhrahuldps
Created April 29, 2020 17:46
Show Gist options
  • Select an option

  • Save singhrahuldps/b6c067f201d36efe1849505854b3bf5a to your computer and use it in GitHub Desktop.

Select an option

Save singhrahuldps/b6c067f201d36efe1849505854b3bf5a to your computer and use it in GitHub Desktop.
// Program to perform mathematical operations on two numbers
double num1,num2;
String operation;
Scanner scanner = new Scanner(System.in);
System.out.println("Enter first number");
num1 = scanner.nextDouble();
System.out.println("Enter second number");
num2 = scanner.nextDouble();
System.out.println("Enter the operation to perform (+,-,*,/)");
operation = scanner.next();
if(operation.equals("+")) { // This code block runs if the condition is true
System.out.println(num1 + num2);
}else if(operation.equals("-")) { // This is only executed if above if fails
System.out.println(num1 - num2);
}else if(operation.equals("*")) {
System.out.println(num1 * num2);
}else{ // if no above conditions match, this is executed
System.out.println(num1 / num2);
}
// Always close the scanner
scanner.close();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment