Showing posts with label calculator in java. Show all posts
Showing posts with label calculator in java. Show all posts

Tuesday, March 3, 2015

Simple Calculator in Java

In this simple and short article I would like to share with you a simple calculator that I wrote using Java as my programming language what the program will do is to ask the user to enter a simple mathematical expression for example 5 + 6 and then the user will press the enter key and it will display the result of 11. It has four basic math functions addition, subtraction, multiplication and division. My program also ask the user if the user would like to give a different value and change new operator by asking the user do you want to continue. The code is very simple primarily I'm just using a switch case statement to select what operator to be used for computation. I hope you will find my work useful and beneficial in learning how to program using Java as your programming language.

If you have some questions please send me an email at jakerpomperada@yahoo.com and jakerpomperada@gmail.com.

People here in the Philippines who wish to contact me can reach me at my mobile number 09173084360


Sample Program Output

Program Listing

import java.util.Scanner;

public class calc {
    public static void main(String[] args) {
Scanner in = new Scanner(System.in);
         char operator,reply;
         do {
              System.out.print("\n");
     System.out.println("==============================================");
     System.out.println("||    <<<   SIMPLE CALCULATOR  >>>          ||");
     System.out.println("==============================================");
              System.out.print("\n");
              System.out.print("ENTER AN EXPRESSION (ex. 2 + 2) :=> ");

              double val1 = in.nextDouble();
              operator=in.next().charAt(0);
              double val2 =in.nextDouble();

            switch (operator)  {
            case '+':
               System.out.println("\n");
               System.out.println("The sum is :=> " + (val1 + val2));
                break;

            case '-':
               System.out.println("\n");
                System.out.println("The difference is :=> " + (val1 - val2));
                break;

            case '/':
                System.out.println("\n");
                System.out.println("The quotient is :=>  " + (val1 / val2));
                break;

            case '*':
                System.out.println("\n");
                System.out.println("The product is :=> " + (val1 * val2));
                break;

            default:
                System.out.println("\n");
                System.out.println("Please Try Again Sorry Wrong Operator...");

            }
                System.out.println("\n");
         System.out.print("Do you Want To Continue (Y/N) :=> ");
          reply=in.next().charAt(0);

   } while(reply=='Y'|| reply=='y');
        System.out.println("\n");
        System.out.println("\t ===== END OF PROGRAM ======");
        System.out.println("\n");
 }
  }