Table of Contents
hide
Example : Write a Switch Case Examples in C to display the Entered value between 1-10.
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
printf("Enter any digit in between 1-10\n");
scanf("%d",&num);
switch (num)
{
case 1:
printf("Entered Value is 1");
break;
case 2:
printf("Entered Value is 2");
break;
case 3:
printf("Entered Value is 3");
break;
case 4:
printf("Entered Value is 4");
break;
case 5:
printf("Entered Value is 5");
break;
case 6:
printf("Entered Value is 6");
break;
case 7:
printf("Entered Value is 7");
break;
case 8:
printf("Entered Value is 8");
break;
case 9:
printf("Entered Value is 9");
break;
case 10:
printf("Entered Value is 10");
break;
default:
printf("Entered value is beyond Range");
}
}
Example : A Switch Case Examples in C to display the name of weekdays from Sunday to Monday using switch/keys 1 to 7.
#include <stdio.h>
#include <conio.h>
void main()
{
int day;
printf("Enter day number from (1-7):\n");
printf("Press 1 to see Sunday:\n");
printf("Press 2 to see Monday and so on ...:\n");
printf("\nEnter/Press your choice button now:\n");
scanf("%d", &day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Oh! You entered a Wrong choice, Pls enter number in between 1-7.");
}
getch();
}
Example : A Switch Case Examples in C to display the name of weekdays from Sunday to Monday using switches 0 to 27.
#include <stdio.h>
#include <conio.h>
void main()
{
int num;
printf("Enter number from (0-27) to see the output:\n");
printf("Press 0-3 to see Sunday:\n");
printf("Press 4-7 to see Monday:\n");
printf("Press 8-11 to see Tuesday:\n");
printf("Press 12-15 to see Wednesday:\n");
printf("Press 16-19 to see Thursday:\n");
printf("Press 20-23 to see Friday:\n");
printf("Press 24-27 to see Saturday:\n");
printf("\nEnter/Press your choice button now:\n");
scanf("%d", &num);
switch(num)
{
case 0: case 1: case 2: case 3:
printf("Sunday");
break;
case 4:
case 5:
case 6:
case 7:
printf("Monday");
break;
case 8: case 9: case 10: case 11:
printf("Tuesday");
break;
case 12: case 13: case 14: case 15:
printf("Wednesday");
break;
case 16: case 17: case 18: case 19:
printf("Thursday");
break;
case 20: case 21: case 22: case 23:
printf("Friday");
break;
case 24: case 25: case 26: case 27:
printf("Saturday");
break;
default:
printf("Oh! You entered a Wrong choice, Pls enter number in between 1-7.");
}
getch();
}
Example : A Switch Case Examples in C to display the name of the month from January to December using unique switch/key letters.
#include<stdio.h>
#include<conio.h>
void main()
{
char x;
clrscr();
printf("Press buttom J/j to print January\n");
printf("Press buttom F/f to print February\n");
printf("Press buttom M/m to print March\n");
printf("Press buttom A/a to print April\n");
printf("Press buttom I/i to print May\n");
printf("Press buttom U/u to print June\n");
printf("Press buttom Y/y to print July\n");
printf("Press buttom E/e to print August\n");
printf("Press buttom S/s to print September\n");
printf("Press buttom O/o to print October\n");
printf("Press buttom N/n to print November\n");
printf("Press buttom D/d to print December\n");
printf ("\nEnter single letter of your choice :");
scanf("%c",&x);
switch(x)
{
case'j':
case'J':
printf("January");
break;
case'f':
case'F':
printf("February");
break;
case'm':
case'M':
printf("March");
break;
case'a':
case'A':
printf("April");
break;
case'i':
case'I':
printf("May");
break;
case'u':
case'U':
printf("June");
break;
case'y':
case'Y':
printf("July");
break;
case'e':
case'E':
printf("August");
break;
case's':
case'S':
printf("September");
break;
case'o':
case'O':
printf("October");
break;
case'n':
case'N':
printf("November");
break;
case'd':
case'D':
printf("December");
break;
default:
printf("Entered choice is incorrect, change it");
}
getch();
}
Example : A Switch Case Examples in C to display the no. of total days in a month from January to December by giving digit of a month.
#include <stdio.h>
#include <conio.h>
void main()
{
int month;
printf("Enter a digit value for month from 1-12: ");
scanf("%d",&month);
switch(month)
{
case 4:
case 6:
case 9:
case 11:
printf("This month is of 30 days");
break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
printf("This month is of 31 days");
break;
case 2:
printf("This month is of 28 days");
break;
default:
printf("Entered value is Incorrect format");
break;
}
getch();
}
Example : A Switch Case Examples in C to display the simple calculator result using + – * / symbols in switch case statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int num1,num2,result;
char symbol;
printf("Press + button to see Addition result:\n");
printf("Press - button to see Subtraction result:\n");
printf("Press * button to see Multiplication result:\n");
printf("Press / button to see Division result:\n");
printf("\nNow Enter or Press your choice button:\n");
scanf("%c",&symbol);
printf("\nEnter two integer value:\n");
scanf("%d%d",&num1,&num2);
switch(symbol)
{
case '+':
result=num1+num2;
printf("%d",result);
break;
case '-':
result=num1-num2;
printf("%d",result);
break;
case '*':
result=num1*num2;
printf("%d",result);
break;
case '/':
result=num1/num2;
printf("%d",result);
break;
default:
printf("Oh no! Entered symbol is incorrect,re-enter correct one");
}
getch();
}
Example : A Switch Case Examples in C to display the simple calculator result using specific letters in a switch case statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int num1,num2,result;
char symbol;
printf("Press A/a button to see Addition result:\n");
printf("Press S/s button to see Subtraction result:\n");
printf("Press M/m button to see Multiplication result:\n");
printf("Press D/d button to see Division result:\n");
printf("\nNow Enter or Press your choice button:\n");
scanf("%c",&symbol);
printf("\nEnter two integer value:\n");
scanf("%d%d",&num1,&num2);
switch(symbol)
{
case 'A':
case 'a':
case '+':
result=num1+num2;
printf("%d",result);
break;
case 'S':
case 's':
case '-':
result=num1-num2;
printf("%d",result);
break;
case 'M':
case 'm':
case '*':
result=num1*num2;
printf("%d",result);
break;
case 'D':
case 'd':
case '/':
result=num1/num2;
printf("%d",result);
break;
default:
printf("Oh no! Entered symbol is incorrect,re-enter correct one");
}
getch();
}
Example : A Switch Case Examples in C to check vowel or consonant letters using a switch case statement.
#include <stdio.h>
#include <conio.h>
void main()
{
char ch;
printf("Enter a character from keyboard: ");
scanf("%c",&ch);
if((ch>='A' && ch<='Z') || (ch>='a' && ch<='z'))
{
switch(ch)
{
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
printf(" The entered character %c is a Capital VOWEL.\n",ch);
break;
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
printf(" The entered character %c is a Small VOWEL.\n",ch);
break;
default:
printf("The entered character %c is a CONSONANT.\n",ch);
}
}
else
{
printf(" The entered character %c is not an alphabet.\n",ch);
}
getch();
}
Example : A Switch Case Examples in C to check the User ID and Password using a nested switch case statement.
#include <stdio.h>
#include <conio.h>
void main()
{
int storeuid=12345, storeupass=14723, id, passwd;
printf("Plese Enter User ID:\n");
scanf("%d",&id);
switch (id)
{
case 12345:
printf("Now enter your correct password:\n");
scanf("%d", &passwd);
switch (passwd)
{
case 14723:
printf("Welcome in C Programming world\n");
break;
default:
printf("Incorrect Password, enter again");
}
break;
default:
printf("Incorrect User Id, enter again");
}
}
Link for more Switch Case Statement
0 Comments