Simple calculator (Console application/Java)

  1. import java.util.*; 

  2. public class calc
  3. {
  4.     public static void main(String[] args)
  5.     {
  6.         Scanner sc = new Scanner(System.in);
  7.         Double v1, v2 = 0.0;
  8.         char decn = 'N';
  9.         do {
  10.         System.out.println("Which operation do you want to perform (Please select a number)\n"
  11.         + "1. Additin\n"
  12.         + "2. Subtraction\n"
  13.         + "3. Multiplication\n"
  14.         + "4. Division\n"
  15.         + "5. Modules\n"
  16.         + "6. Power");
  17.         int ops = sc.nextInt();
  18.         switch (ops){
  19.         case 1:
  20.         System.out.println("Please enter the first nubmer: ");
  21.         v1 = sc.nextDouble();
  22.         System.out.println("Please enter the second nubmer: ");
  23.         v2 = sc.nextDouble();
  24.         System.out.println("The sum of (" + v1 + ") and (" + v2 + ") is (" + (v1 + v2) + ")");
  25.         break;
  26.         case 2:
  27.         System.out.println("Please enter the first nubmer: ");
  28.         v1 = sc.nextDouble();
  29.         System.out.println("Please enter the second nubmer: ");
  30.         v2 = sc.nextDouble();
  31.         System.out.println("The difference between (" + v1 + ") and (" + v2 + ") is (" + (v1 - v2) + ")");
  32.         break;
  33.         case 3:
  34.         System.out.println("Please enter the first nubmer: ");
  35.         v1 = sc.nextDouble();
  36.         System.out.println("Please enter the second nubmer: ");
  37.         v2 = sc.nextDouble();
  38.         System.out.println("The product of (" + v1 + ") and (" + v2 + ") is (" + (v1 * v2) + ")");
  39.         break;
  40.         case 4:
  41.         System.out.println("Please enter the first nubmer: ");
  42.         v1 = sc.nextDouble();
  43.         System.out.println("Please enter the second nubmer: ");
  44.         v2 = sc.nextDouble();
  45.         System.out.println("The quotient dividing (" + v1 + ") by (" + v2 + ") is (" + (v1 / v2) + ")");
  46.         break;
  47.         case 5:
  48.         System.out.println("Please enter the first nubmer: ");
  49.         v1 = sc.nextDouble();
  50.         System.out.println("Please enter the second nubmer: ");
  51.         v2 = sc.nextDouble();
  52.         System.out.println("The remainder dividing (" + v1 + ") by (" + v2 + ") is (" + (v1 % v2) + ")");
  53.         break;
  54.         case 6:
  55.         System.out.println("Please enter any nubmer: ");
  56.         v1 = sc.nextDouble();
  57.         System.out.println("Please enter the power of: ");
  58.         v2 = sc.nextDouble();
  59.         System.out.println("The power of (" + v1 + ") is (" + Math.pow(v1, v2) + ")");
  60.         break;
  61.         default:
  62.         System.out.println("Oh'ho you have entered something wrong");
  63.         break;
  64.         }
  65.         System.out.println("Do you want to try again? if yes then type 'Y'");
  66.         decn = sc.next().charAt(0);
  67.         } while(decn == 'Y' || decn == 'y');
  68.         sc.close();
  69.     }
  70. }























































Comments

Popular posts from this blog

String class in C# (Methods and properties)

SQL Server Queries