Skip to main content

Switch Case

Simple Calculator program using switch case

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
47
48
49
50
51
52
53
54
55
 
package basic;
 
import java.util.Scanner;
 
public class Javaswitch 
{
 
 public static void main(String arg[])
  {
 
      int x;
      int a,b;
 
      Scanner c=new Scanner(System.in);
      System.out.println("enter value of a");
      a=c.nextInt();
      System.out.println("enter value of b");
      b=c.nextInt();
      System.out.println("enter operation u want to perform
                         1.addtion 2.multiplication 3.subtraction");
      x=c.nextInt();
 
     int answer = 0;
 
	 switch (x) 
            {
		case 1:
			answer=a+b;
			System.out.println("addition is ="+answer);
			break;
 
		case 2:
			answer=a*b;
			System.out.println("multiplication is=" +answer);
 
		case 3:
			answer=a-b;
			System.out.println("subtration is ="+answer);
 
		default:
			System.out.println("invalid operator");
			break;
		}
	}
}
 
output:
enter value of a
5
enter value of b
5
enter operation u want to perform 1.addtion 2.multiplication 3.subtraction
1
addition is =10