switch(variable) { case value: --------- --------- break; case value: --------- --------- break; . . default: --------- --------- }
#include⁢stdio.h> main() { int a; printf("Enter a single digit number"); scanf("%d",&a); switch(a) { case 0: printf("Zero");break; case 1: printf("One");break; case 2: printf("Two");break; case 3: printf("Three");break; case 4: printf("Four");break; case 5: printf("Five");break; case 6: printf("Six");break; case 7: printf("Seven");break; case 8: printf("Eight");break; case 9: printf("Nine");break; default : printf("Not a single digit number"); } }
Enter a single digit number 7 Seven
#include⁢stdio.h> main() { int a; printf("Enter month in digit "); scanf("%d",&a); switch(a) { case 1 : printf("Jan");break; case 2 : printf("Feb");break; case 3 : printf("March");break; case 4 : printf("April");break; case 5 : printf("May");break; case 6 : printf("June");break; case 7 : printf("July");break; case 8 : printf("Aug");break; case 9 : printf("Sept");break; case 10 : printf("Oct");break; case 11 : printf("Nov");break; case 12 : printf("Dec");break; default : printf("Invalid Month"); } }
Enter month in digit 3 March
#include⁢stdio.h> main() { char a; printf("Enter Color Code "); scanf("%c",&a); switch(a) { case 'R' : case 'r' : printf("Red"); break; case 'G' : case 'g' : printf("Green") ; break; case 'B' : case 'b' : printf("Blue"); break; default : printf("White") ; } }
Enter Color Code g Green
#include⁢stdio.h> main() { float a,b,c; char o; printf("Enter simple Exp. : "); scanf("%f %c %f",&a,&o,&b); switch(o) { case '+': c=a+b;break; case '-': c=a-b;break; case '*': c=a*b;break; case '/': c=a/b;break; } printf("= %f",c); }
Enter simple Exp: 2.4 + 7.1 = 11.5
#include⁢stdio.h> main() { int n; printf("Enter temp in range 0-99:"); scanf("%d",&n); switch(n/20) { case 0: printf("Cold"); break; case 1: printf("normal"); break; case 2: printf("warm"); break; case 3: printf("hot"); break; case 4: printf("very hot");break; } }
Enter temp in range 0-99:60 hot