Simple Calculator in Java
Here you will learn to create a Calculator in Java programming.
This program describes the uses of switch statement with 4 case (1. Add, 2. Sub, 3. Mul, 4. Div). When particular case value matches code related with that case will be executed otherwise default case will be executed.
Program to create Calculator in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class calculator { public static void main(String str[]) { int a,b,c,d,k=0; a=Integer.parseInt(str[0]); b=Integer.parseInt(str[1]); c=Integer.parseInt(str[2]); do { switch(a) { case 1: { d=b+c; System.out.println("addition= "+d); break; } case 2: { d=b-c; System.out.println("subtraction= "+d); break; } case 3: { d=b*c; System.out.println("multiplication= "+d); break; } case 4: { d=b/c; System.out.println("division= "+d); break; } default : { System.out.println("Enter correct choice"); } } k++; } while(k<=0); } } |
Output
Check out our other Java Programming Examples