Advertisement
LegendSujay2019

Program to make Calculator using Java

Aug 26th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.03 KB | None | 0 0
  1. /* Java Program Example - Make Calculator */
  2.        
  3. import java.util.Scanner;
  4.  
  5. public class JavaProgram
  6. {
  7.     public static void main(String args[])
  8.     {
  9.         float a, b, res;
  10.         char choice, ch;
  11.         Scanner scan = new Scanner(System.in);
  12.        
  13.         do
  14.         {
  15.             System.out.print("1. Addition\n");
  16.             System.out.print("2. Subtraction\n");
  17.             System.out.print("3. Multiplication\n");
  18.             System.out.print("4. Division\n");
  19.             System.out.print("5. Exit\n\n");
  20.             System.out.print("Enter Your Choice : ");
  21.             choice = scan.next().charAt(0);
  22.             switch(choice)
  23.             {
  24.                 case '1' : System.out.print("Enter Two Number : ");
  25.                     a = scan.nextFloat();
  26.                     b = scan.nextFloat();
  27.                     res = a + b;
  28.                     System.out.print("Result = " + res);
  29.                     break;
  30.                 case '2' : System.out.print("Enter Two Number : ");
  31.                     a = scan.nextFloat();
  32.                     b = scan.nextFloat();
  33.                     res = a - b;
  34.                     System.out.print("Result = " + res);
  35.                     break;
  36.                 case '3' : System.out.print("Enter Two Number : ");
  37.                     a = scan.nextFloat();
  38.                     b = scan.nextFloat();
  39.                     res = a * b;
  40.                     System.out.print("Result = " + res);
  41.                     break;
  42.                 case '4' : System.out.print("Enter Two Number : ");
  43.                     a = scan.nextFloat();
  44.                     b = scan.nextFloat();
  45.                     res = a / b;
  46.                     System.out.print("Result = " + res);
  47.                     break;
  48.                 case '5' : System.exit(0);
  49.                     break;
  50.                 default : System.out.print("Wrong Choice!!!");
  51.                     break;
  52.             }
  53.             System.out.print("\n---------------------------------------\n");
  54.         }while(choice != 5);      
  55.     }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement