Header Ads

How to make calculator by Java?

Algorithm:


  1. Declare two variables for the operands and one variable for the operator.
  2. Read the operands and operator from the user.
  3. Use a switch statement to perform the appropriate arithmetic operation based on the operator entered by the user.
  4. Display the result of the operation to the user.
This implementation prompts the user to enter two numbers and an operator, then uses a switch statement to perform the appropriate arithmetic operation and display the result. Note that this implementation assumes that the user enters valid input. In a more robust implementation, you would need to add error handling to ensure that the user enters valid input.


Code:


import java.util.Scanner;

public class calculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int c;
        double n1, n2;
        System.out.println("1. Add \n 2. Subtract \n 3. Multiply \n 4. Divide");
        c = scanner.nextInt();
        System.out.print("Enter First Number : ");
        n1 = scanner.nextDouble();
        System.out.print("Enter Second Number : ");
        n2 = scanner.nextDouble();

        double result = 0;
        switch(c) {
            case 1:
                result = n1 + n2;
                break;
            case 2:
                result = n1 - n2;
                break;
            case 3:
                result = n1 * n2;
                break;
            case 4:
                result = n1 / n2;
                break;
            default:
                System.out.println("Invalid Input");
                break;
        }
        System.out.println("Result : "+result);
    }
}  


Output:

1. Add 
 2. Subtract
 3. Multiply
 4. Divide
1
Enter First Number : 10
Enter Second Number : 20
Result : 30.0

Explanation:

This is a simple Java program that performs basic arithmetic operations based on user input. Here's a breakdown of what the code does:
  1. The program starts by importing the Scanner class, which allows the program to read user input from the command line.
  2. The public class calculator defines the class name for the program.
  3. The main method is the entry point for the program. It first creates a new Scanner object to read input from the user.
  4. It then declares three variables: c, n1, and n2. c is an integer that stores the user's choice of arithmetic operation (1 for addition, 2 for subtraction, 3 for multiplication, and 4 for division). n1 and n2 are doubles that store the two numbers to be used in the arithmetic operation.
  5. The program then prompts the user to choose an operation by printing a menu of options. It uses the nextInt() method of the Scanner object to read the user's input and stores it in the c variable.
  6. The program then prompts the user to enter two numbers, using the nextDouble() method of the Scanner object to read each number and store it in the n1 and n2 variables.
  7. The program then declares a variable result and sets it to 0.
  8. The program then uses a switch statement to perform the arithmetic operation based on the user's choice of operation. If the user entered an invalid choice, the default case prints an error message.
  9. After performing the arithmetic operation, the program prints the result using the println() method, concatenating the result with the string "Result : ".
  10. The program then ends.
Overall, this program is a simple demonstration of how to use a switch statement in Java to perform different operations based on user input.






Related Links:

Post a Comment

0 Comments