Untitled

mail@pastecode.io avatar
unknown
plain_text
18 days ago
3.1 kB
7
Indexable
Never
import java.util.Scanner; // Add Scanner class to allow user input from console

public class Main
{
    public static void main(String[] args)
    {
        // Create a Scanner object to read user input
        Scanner scanner = new Scanner(System.in);

        // Ask for the operation
        System.out.println("\n1. Addition\n2. Subtraction\n3. Multiplication\n4. Division\n5. Exponent\n6. Square Root\n\nEnter 1-6 for the Operation: ");
        char choice = scanner.next().charAt(0);
        String operation = "";

        // Ask for the first number
        System.out.println("\nEnter Number 1: ");
        double num1 = scanner.nextDouble();

        // Ask for the second number
        System.out.println("\nEnter Number 2: ");
        double num2 = scanner.nextDouble();

        // Variable to store the result
        double result;

        // Perform calculation based on the operation
        if (choice == '1') // Addition
        {
            result = num1 + num2;
            operation = " + ";
            System.out.println("Answer: " + num1 + operation + num2 + " = " + result);
        }
        else if (choice == '2') // Subtraction
        {
            result = num1 - num2;
            operation = " - ";
            System.out.println("Answer: " + num1 + operation + num2 + " = " + result);
        }
        else if (choice == '3') // Multiplication
        {
            result = num1 * num2;
            operation = " * ";
            System.out.println("Answer: " + num1 + operation + num2 + " = " + result);
        }
        else if (choice == '4') // Division
        {
            // Make sure they don't divide by 0
            if (num2 != 0) {
                result = num1 / num2;
                operation = " / ";
                System.out.println("Answer: " + num1 + operation + num2 + " = " + result);
            }
            else
            {
                System.out.println("Error: Division by zero is not allowed.");
            }
        }
        else if (choice == '5') // Exponent
        {
            // Exponentiation: num1 raised to the power of num2
            result = Math.pow(num1, num2);
            operation = " ^ ";
            System.out.println("Answer: " + num1 + operation + num2 + " = " + result);

        }
        else if (choice == '6') // Square Root
        {
            // Square root: Only uses num1
            if (num1 >= 0)
            {
                result = Math.sqrt(num1);
                System.out.println("\nAnswer: √" + num1 + " = " + result);
            } else
            {
                System.out.println("Error: Cannot compute the square root of a negative number.");
            }

        } else
        {
            System.out.println("\n\nError: Invalid operation.\n\nSolution: Please use 1-6 for the Operation");
        }

        System.out.println("\nThank you for using my Calculator!");

        // Close the scanner
        scanner.close();
    }
}
Leave a Comment