❓ Help BOT help

Yayann

Honorary Poster
Please submit a code on JAVA that would do the following
1. ask user to input 2 numbers
2. ask user to input any arithmetic E×ρréššion - addition, subtraction, multiplication, division, modulo
3. show the result based on the arithmetic E×ρréššion selected
 
Solution
🤖
Sure! Here's an example Java code that achieves the functionality you described:

Java:
import java.util.Scanner;

public class ArithmeticCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Ask user to input 2 numbers
        System.out.print("Enter first number: ");
        double num1 = scanner.nextDouble();

        System.out.print("Enter second number: ");
        double num2 = scanner.nextDouble();

        // Step 2: Ask user to input arithmetic E×ρréššion
        System.out.println("Select an arithmetic operation:");
        System.out.println("1. Addition (+)");
        System.out.println("2. Subtraction (-)");
        System.out.println("3. Multiplication...
Sure! Here's an example Java code that achieves the functionality you described:

Java:
import java.util.Scanner;

public class ArithmeticCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Step 1: Ask user to input 2 numbers
        System.out.print("Enter first number: ");
        double num1 = scanner.nextDouble();

        System.out.print("Enter second number: ");
        double num2 = scanner.nextDouble();

        // Step 2: Ask user to input arithmetic E×ρréššion
        System.out.println("Select an arithmetic operation:");
        System.out.println("1. Addition (+)");
        System.out.println("2. Subtraction (-)");
        System.out.println("3. Multiplication (*)");
        System.out.println("4. Division (/)");
        System.out.println("5. Modulo (%)");

        int operation = scanner.nextInt();
        double result;

        // Step 3: Perform arithmetic operation and show the result
        switch (operation) {
            case 1:
                result = num1 + num2;
                System.out.println("Addition Result: " + result);
                break;
            case 2:
                result = num1 - num2;
                System.out.println("Subtraction Result: " + result);
                break;
            case 3:
                result = num1 * num2;
                System.out.println("Multiplication Result: " + result);
                break;
            case 4:
                if (num2 != 0) {
                    result = num1 / num2;
                    System.out.println("Division Result: " + result);
                } else {
                    System.out.println("Error: Division by zero is not allowed!");
                }
                break;
            case 5:
                result = num1 % num2;
                System.out.println("Modulo Result: " + result);
                break;
            default:
                System.out.println("Invalid operation selected!");
        }
    }
}

When you run this code, it will prompt the user to enter two numbers and then select an arithmetic operation. After the user makes their selection, the code will perform the corresponding operation and display the result.

Note that this code assumes the user will input valid numbers (doubles) and select a valid arithmetic operation. You may want to add additional input validation if needed.
 
Solution

About this Thread

  • 1
    Replies
  • 239
    Views
  • 1
    Participants
Last reply from:
Unknown user

Trending Topics

Online now

Members online
307
Guests online
1,195
Total visitors
1,502

Forum statistics

Threads
2,273,066
Posts
28,947,367
Members
1,236,578
Latest member
Jlbbravo
Back
Top