Skip to content

U23EEE1033 #576

@muhayuba406

Description

@muhayuba406

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

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