Switch Statement in C
Get Certified in C Programming and Take Your Skills to the Next Level
Program 1
// Program for switch case statements
#include<stdio.h>
#include<conio.h>
int main()
{
// Program for word format of a number
system("cls");
int n;
printf("Enter a number");
scanf("%d",&n);
switch(n)
{
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;
default:printf("Invalid number");
}
return 0;
}
Program 2
// Program for switch case statements
#include<stdio.h>
#include<conio.h>
int main()
{
// Program for Vowel
system("cls");
char ch;
printf("Enter a character");
ch=getche();
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':printf("\nVowel");break;
default:printf("\nNot a Vowel");
}
return 0;
}
Program 3
// Program for switch case statements
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b,c,choice;
system("cls");
printf("\n-------------------Menu--------------------");
printf("\n\t1. Addition");
printf("\n \t2. Swap");
printf("\n \t3. Grater");
printf("\n \t4. Exit");
printf("\n---------------------------------------------");
printf("\n Enter a choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\nEnter two Number");
scanf("%d %d",&a,&b);
c=a+b;
printf("\nAddition is %d",c);
break;
}
case 2:
{
printf("\nEnter two Number");
scanf("%d %d",&a,&b);
printf("\n Before swaping %d %d",a,b);
a=a+b;
b=a-b;
a=a-b;
printf("\n After swaping %d %d",a,b);
break;
}
case 3:
{
printf("\nEnter two Number");
scanf("%d %d",&a,&b);
if(a>b)
printf("\nGrater number is: %d",a);
else
printf("\nGrater number is: %d",b);
}
}
return 0;
}
Did we exceed your expectations?
If Yes, share your valuable feedback on Google

