Tuesday, October 19, 2021

Calculator Using Switch Statement in Java

 Machine Problem

 Write a Java program that will use the MDAS Operation to  * calculate integers. "Use switch Statement" using Java programming language.

  Sample Program Output

  Operator (+,-,*,/)

 Choose Operator : +

  Enter First Number: 10

  Enter Second Number: 1

  10 + 1 = 11

  Operator (+,-,*,/)

 Choose Operator : -

  Enter First Number: 10

  Enter Second Number: 1

  10 - 1 = 9

 I am currently accepting programming work, IT projects, school and application development, programming projects, thesis and capstone projects, IT consulting work, computer tutorials, and web development work kindly contact me at the following email address for further details.  If you want to advertise on my website kindly contact me also in my email address also. Thank you.

My email address is the following jakerpomperada@gmail.com, jakerpomperada@aol.com, and jakerpomperada@yahoo.com.

My mobile number here in the Philippines is 09173084360.





Program Listing

package test;


import java.util.Scanner;



/*

 * Calculator.java

 * 

 * Jake Rodriguez Pomperada, MAED-IT, MIT

 * www.jakerpomperada.com  and www.jakerpomperada.blogspot.com

 * jakerpomperada@gmail.com

 * Bacolod City, Negros Occidental Philippines

 * 

 * Machine Problem

 * 

 * Write a Java program that will use the MDAS Operation to

 * calculate integers. "Use switch Statement"

 * 

 * Sample Program Output

 * 

 * Operator (+,-,*,/)

 * Choose Operator : +

 * Enter First Number: 10

 * Enter Second Number: 1

 * 10 + 1 = 11

 * 

 * Operator (+,-,*,/)

 * Choose Operator : -

 * Enter First Number: 10

 * Enter Second Number: 1

 * 10 - 1 = 9

 */


public class Calculator {

public static void main(String[] args) {


char operator;

    int number1, number2, result;


    // create an object of Scanner class

    Scanner input = new Scanner(System.in);


    // ask users to enter operator

    System.out.println();

    System.out.println("+,=,*,/");

    System.out.print("Choose Operator : ");

    operator = input.next().charAt(0);


    // ask users to enter numbers

    System.out.print("Enter First Number : ");

    number1 = input.nextInt();


    System.out.print("Enter Second Number : ");

    number2 = input.nextInt();


    switch (operator) {


      // performs addition between numbers

      case '+':

        result = number1 + number2;

        System.out.println(number1 + " + " + number2 + " = " + result);

        break;


      // performs subtraction between numbers

      case '-':

        result = number1 - number2;

        System.out.println(number1 + " - " + number2 + " = " + result);

        break;


      // performs multiplication between numbers

      case '*':

        result = number1 * number2;

        System.out.println(number1 + " * " + number2 + " = " + result);

        break;


      // performs division between numbers

      case '/':

        result = number1 / number2;

        System.out.println(number1 + " / " + number2 + " = " + result);

        break;


      default:

        System.out.println("Invalid operator!");

        break;

    }


    input.close();

  }

}



No comments:

Post a Comment