-
Notifications
You must be signed in to change notification settings - Fork 605
Open
Description
import java.util.Scanner;
public class calculator {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
// Input first number
System.out.print("Enter first integer: ");
int x = scan.nextInt();
// Input second number
System.out.print("Enter second integer: ");
int y = scan.nextInt();
// Choosing operation
System.out.println("Choose an operation: ");
System.out.println("1 - Addition\n2 - Subtraction\n3 - Division\n4 - Multiplication\n5 - Modulus");
System.out.print("Enter your choice (1-5): ");
int choice = scan.nextInt();
// Perform operation based on user input
switch (choice) {
case 1:
System.out.println("Result: " + (x + y));
break;
case 2:
System.out.println("Result: " + (x - y));
break;
case 3:
if (y != 0) {
System.out.println("Result: " + (x / y));
} else {
System.out.println("Error: Division by zero is not allowed.");
}
break;
case 4:
System.out.println("Result: " + (x * y));
break;
case 5:
if (y != 0) {
System.out.println("Result: " + (x % y));
} else {
System.out.println("Error: Modulus by zero is not allowed.");
}
break;
default:
System.out.println("Invalid choice. Please select a number between 1 and 5.");
}
scan.close();
}
}
Metadata
Metadata
Assignees
Labels
No labels