Q:

Write a Java Program to Perform Mathematical Operations

belongs to collection: Java Basic Solved Programs

0

Java Program to Make a Simple Calculator using switch case in Java Programming which performs the basic four mathematical operations i.e., addition, subtraction, multiplication, and division.

Following is a simple Java Program which is a menu-driven program based on simple calculation like addition, subtraction, multiplication and division according to user’s choice:

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

SOURCE CODE ::

import java.util.Scanner;

public class Calculator
{
    public static void main(String args[])
    {
        float a, b, res;
        char choice, ch;
        Scanner scan = new Scanner(System.in);
                
        do
        {
            System.out.print("1. Addition\n");
            System.out.print("2. Subtraction\n");
            System.out.print("3. Multiplication\n");
            System.out.print("4. Division\n");
            System.out.print("5. Exit\n\n");
            System.out.print("Enter Your Choice : ");
            choice = scan.next().charAt(0);
            switch(choice)
            {
                case '1' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a + b;
                    System.out.print("Result = " + res);
                    break;
                case '2' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a - b;
                    System.out.print("Result = " + res);
                    break;
                case '3' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a * b;
                    System.out.print("Result = " + res);
                    break;
                case '4' : System.out.print("Enter Two Number : ");
                    a = scan.nextFloat();
                    b = scan.nextFloat();
                    res = a / b;
                    System.out.print("Result = " + res);
                    break;
                case '5' : System.exit(0);
                    break;
                default : System.out.print("Wrong Choice!!!");
                    break;
            }
            System.out.print("\n---------------------------------------\n");
        }while(choice != 5);       
    }
}

OUTPUT ::

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 1
Enter Two Number : 43
23
Result = 66.0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 4
Enter Two Number : 88
11
Result = 8.0
---------------------------------------
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit

Enter Your Choice : 5

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Basic Solved Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a Java Program to Calculate Arithmetic Mean ... >>
<< Write a Java Program to Perform Mathematical Opera...