Java program to demonstrate 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
56
57
58
59
60
/* switch case demo
   Example :
   Input - 124
   Output - One Two Four */
class SwitchCaseDemo{
	public static void main(String args[]){
		try{
			int num = Integer.parseInt(args[0]);
			int n = num; //used at last time check
			int reverse=0,remainder;
			while(num > 0){
				remainder = num % 10;
				reverse = reverse * 10 + remainder;
				num = num / 10;
			}
			String result=""; //contains the actual output
			while(reverse > 0){
				remainder = reverse % 10;
				reverse = reverse / 10;
				switch(remainder){
					case 0 :
						result = result + "Zero ";
						break;
					case 1 :
						result = result + "One ";
						break;
					case 2 :
						result = result + "Two ";
						break;
					case 3 :
						result = result + "Three ";
						break;
					case 4 :
						result = result + "Four ";
						break;
					case 5 :
						result = result + "Five ";
						break;
					case 6 :
						result = result + "Six ";
						break;
					case 7 :
						result = result + "Seven ";
						break;
					case 8 :
						result = result + "Eight ";
						break;
					case 9 :
						result = result + "Nine ";
						break;
					default:
						result="";
				}
			}
			System.out.println(result);
		}catch(Exception e){
			System.out.println("Invalid Number Format");
		}
	}
}

One Response to “Java program to demonstrate switch case”

  1. here u take 124 as example…..if u take number thats end in 0 as a input(example 120) then it wont work….zero is nt gtng printed on console….m able to see only: one two

    Reply

Leave a Reply