In this article switch case programs in c we give the information about switch case related programs and related output in details.
Switch case Programs in C
// 1. Write a C program to create a simple calculator using switch statements
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=0,ch=0;
clrscr();
printf(“\n\n Enter the two numbers: “);
scanf(“%d%d”,&a,&b);
printf(“\n 1: Addition \n 2: Subtraction \n 3: Multiplication \n 4: Division”);
printf(“\n Enter your Choice: “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n Addition of two numbers: %d”,a+b);
break;
case 2:
printf(“\n Subtraction of two numbers: %d”,a-b);
break;
case 3:
printf(“\n Multiplication of two numbers: %d”,a*b);
break;
case 4:
printf(“\n Division of two numbers: %d”,a/b);
break;
default:
printf(“\n enter only 1 to 4 number…”);
}
getch();
}
Output:
Enter the two numbers: 10
3
1: Addition
2: Subtraction
3: Multiplication
4: Division
Enter your Choice: 3
Multiplication of two numbers: 30
// 2. Write a C program to show day’s name
#include<stdio.h>
#include<conio.h>
void main()
{
int ch=0;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n Sunday”);
break;
case 2:
printf(“\n Monday”);
break;
case 3:
printf(“\n Tuesday”);
break;
case 4:
printf(“\n Wednesday”);
break;
case 5:
printf(“\n Thursday”);
break;
case 6:
printf(“\n Friday”);
break;
case 7:
printf(“\n Saturday”);
break;
default:
printf(“\n Enter only 1 to 7 number…”);
}
getch();
}
Output:
Enter the number: 2
Monday
// 3. Write a C program to show vowel and consonants for given alphabet
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
void main()
{
char a,b;
clrscr();
printf(“\n Enter the character: “);
scanf(“%c”,&a);
b=tolower(a);
switch(b)
{
case ‘a’:
case ‘e’:
case ‘i’:
case ‘o’:
case ‘u’:
printf(“\n\n %c is a Vowel.”, b);
break;
default:
printf(“\n\n %c is a Consonants…”,b);
}
getch();
}
Output:
Enter the character: e
e is a Vowel.
// 4. Write a C program to find different areas using switch statements
#include<stdio.h>
#include<conio.h>
void main()
{
int a=0,b=0,ch=0;
clrscr();
printf(“\n 1: Area of Rectangle \n 2: Area of Circle \n 3: Area of Square \n 4: Area of Triangle”);
printf(“\n\n Enter the two numbers: “);
scanf(“%d%d”,&a,&b);
printf(“\n Enter your Choice: “);
scanf(“%d”,&ch);
switch(ch)
{
case 1:
printf(“\n Area of Rectangle: %d”,a*b);
break;
case 2:
printf(“\n Area of Circle: %.2f”,3.142*a*a);
break;
case 3:
printf(“\n Area of Square: %d”,a*a);
break;
case 4:
printf(“\n Area of Triangle: %d”, 0.5*a*b);
break;
default:
printf(“\n enter only 1 to 4 number…”);
}
getch();
}
Output:
1: Area of Rectangle
2: Area of Circle
3: Area of Square
4: Area of Triangle
Enter the two numbers: 5
7
Enter your Choice: 1
Area of Rectangle: 35
// 5. Write a C program to show seasons using switch statements
#include<stdio.h>
#include<conio.h>
void main()
{
int ch=0;
clrscr();
printf(“\n Enter the number: “);
scanf(“%d”,&ch);
switch(ch)
{
case 6:
case 7:
case 8:
case 9:
printf(“\n Rainy Season.”);
break;
case 10:
case 11:
case 12:
case 1:
printf(“\n Winter seasons.”);
break;
case 2:
case 3:
case 4:
case 5:
printf(“\n Summer seasons.”);
break;
default:
printf(“\n enter only 1 to 12 number…”);
}
getch();
}
Output:
Enter the number: 5
Summer seasons.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining