Java program of Calculator

Introduction:

The provided Java program is a simple calculator that allows users to perform basic arithmetic operations, such as addition, subtraction, multiplication, and division. It interacts with the user through a console interface, providing a menu for operation selection and prompting the user to enter two numbers for the chosen operation. The program then displays the result of the operation or an error message in the case of division by zero.

Explanation:

The program utilizes the Scanner class to take user input and employs a modular approach with functions for each arithmetic operation (addition, subtraction, multiplication, and division). The main method acts as the entry point, guiding the user through the process of selecting an operation and entering numerical values. The switch statement is employed to execute the appropriate operation based on the user’s choice, and the program ensures that division by zero is handled to prevent runtime errors.

Algorithm:

• Print a menu displaying available arithmetic operations: add, subtract, multiply, and divide.
• Prompt the user to enter a choice (1-4).
2. Input Numbers:
• Prompt the user to enter two numbers for the chosen operation.
3. Perform Operation:
• Use a switch statement to determine the selected operation.
• Call the corresponding function (add, subtract, multiply, or divide) with the provided numbers.
• Display the result or an error message for division by zero.
4. Close Scanner:
• Close the Scanner object to release resources.
5. Arithmetic Functions:
• Separate functions for addition, subtraction, multiplication, and division perform the respective mathematical operations.

Code :

```    ```
import java.util.Scanner;
public class Calculator {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Simple Calculator");
System.out.println("2. Subtract");
System.out.println("3. Multiply");
System.out.println("4. Divide");
int choice = scanner.nextInt();
System.out.print("Enter first number: ");
double num1 = scanner.nextDouble();
System.out.print("Enter second number: ");
double num2 = scanner.nextDouble();
switch (choice) {
case 1:
break;
case 2:
System.out.println("Result: " + subtract(num1, num2));
break;
case 3:
System.out.println("Result: " + multiply(num1, num2));
break;
case 4:
if (num2 != 0) {
System.out.println("Result: " + divide(num1, num2));
} else {
System.out.println("Error: Cannot divide by zero");
}
break;
default:
System.out.println("Invalid choice");
}
scanner.close();
}
public static double add(double num1, double num2) {
return num1 + num2;
}
public static double subtract(double num1, double num2) {
return num1 - num2;
}
public static double multiply(double num1, double num2) {
return num1 * num2;
}
public static double divide(double num1, double num2) {
return num1 / num2;
}
}

```
```

Output :

```    ```
Simple Calculator
2. Subtract
3. Multiply
4. Divide
```
```

Input :

```    ```
Enter first number: 34
Enter second number: 6789
Result: 6823.0
```
```

Summary:

This program offers a straightforward and interactive way for users to perform basic arithmetic calculations. Its modular design and user-friendly interface make it easy to understand and use. However, it’s important to note that this calculator is limited to simple operations and doesn’t include advanced functionalities or error handling beyond division by zero.

This program allows the user to choose an operation (addition, subtraction, multiplication, or division) and then enter two numbers to perform the selected operation. Note that this is a basic example, and you may want to add more error handling and features based on your requirements.

FAQ's

1. Question: What does the Java program for a calculator do?
• Answer: The Java program for a calculator provides a simple interactive platform for performing basic arithmetic operations such as addition, subtraction, multiplication, and division.
1. Question: How can I create a Java program for a calculator?
• Answer: To create a Java program for a calculator, you can use the Scanner class for user input and implement functions for addition, subtraction, multiplication, and division. The program guides users through menu options to perform desired calculations.
1. Question: Is there a specific Java program calculator example available?
• Answer: Yes, the provided Java program is a simple calculator example. It allows users to choose an operation, input two numbers, and then displays the result or an error message for division by zero.
1. Question: What features does the Java program simple calculator include?
• Answer: The Java program for a simple calculator includes features such as a user-friendly menu, input validation, and functions for basic arithmetic operationsâ€”addition, subtraction, multiplication, and division.
1. Question: Where can I find a basic calculator Java program code?
• Answer: The basic calculator Java program code is available in the provided example. It includes a modular structure with functions for each operation, making it a good starting point for understanding and creating your own calculator programs in Java.