Simple calculator (Console application/Java)
- import java.util.*;
- public class calc
- {
- public static void main(String[] args)
- {
- Scanner sc = new Scanner(System.in);
- Double v1, v2 = 0.0;
- char decn = 'N';
- do {
- System.out.println("Which operation do you want to perform (Please select a number)\n"
- + "1. Additin\n"
- + "2. Subtraction\n"
- + "3. Multiplication\n"
- + "4. Division\n"
- + "5. Modules\n"
- + "6. Power");
- int ops = sc.nextInt();
- switch (ops){
- case 1:
- System.out.println("Please enter the first nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the second nubmer: ");
- v2 = sc.nextDouble();
- System.out.println("The sum of (" + v1 + ") and (" + v2 + ") is (" + (v1 + v2) + ")");
- break;
- case 2:
- System.out.println("Please enter the first nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the second nubmer: ");
- v2 = sc.nextDouble();
- System.out.println("The difference between (" + v1 + ") and (" + v2 + ") is (" + (v1 - v2) + ")");
- break;
- case 3:
- System.out.println("Please enter the first nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the second nubmer: ");
- v2 = sc.nextDouble();
- System.out.println("The product of (" + v1 + ") and (" + v2 + ") is (" + (v1 * v2) + ")");
- break;
- case 4:
- System.out.println("Please enter the first nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the second nubmer: ");
- v2 = sc.nextDouble();
- System.out.println("The quotient dividing (" + v1 + ") by (" + v2 + ") is (" + (v1 / v2) + ")");
- break;
- case 5:
- System.out.println("Please enter the first nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the second nubmer: ");
- v2 = sc.nextDouble();
- System.out.println("The remainder dividing (" + v1 + ") by (" + v2 + ") is (" + (v1 % v2) + ")");
- break;
- case 6:
- System.out.println("Please enter any nubmer: ");
- v1 = sc.nextDouble();
- System.out.println("Please enter the power of: ");
- v2 = sc.nextDouble();
- System.out.println("The power of (" + v1 + ") is (" + Math.pow(v1, v2) + ")");
- break;
- default:
- System.out.println("Oh'ho you have entered something wrong");
- break;
- }
- System.out.println("Do you want to try again? if yes then type 'Y'");
- decn = sc.next().charAt(0);
- } while(decn == 'Y' || decn == 'y');
- sc.close();
- }
- }
Comments
Post a Comment