Skip to content

Monday Caleb Ufot U23AUE1031  #373

Description

@Calebmonday

import java.util.Scanner;

public class operation {
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();
}

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions