C Programs Example on Switch Case

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest in 3 Numbers
Find Grade of student
Find the absolute value
Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Previously we learned about switch case statements and now, we will write the switch case example in C.

Table Of Contents


Switch Case Example in C: Odd-Even

Program:- Write a C program using a switch case to check whether the number entered by the user is odd or even.

#include<stdio.h>
int main()
{
  int number;

  printf("Enter a number: ");
  scanf("%d",&number);

  switch( number%2 )
  {
    case 0:
      printf("%d is even.", number);
      break;
    case 1:
      printf("%d is odd.", number);
  }

  return 0;
}

Output for the different test-cases:-

Enter a number: 8
8 is even.

Enter a number: 9
9 is odd.

switch case example in c

Switch case example in C: Check character is vowel or not

Program:- Write a C program to checks whether a character entered by the user is a vowel or not by using the switch case statement.

#include<stdio.h>
int main()
{
   char ch;

   printf("Enter a character: ");
   scanf("%c", &ch);

   switch(ch)
   {
     case 'a':
     case 'e':
     case 'i':
     case 'o':
     case 'u':
     case 'A':
     case 'E':
     case 'I':
     case 'O':
     case 'U':
       printf("%c is vowel.", ch);
       break;
     default:
       printf("%c is not a vowel.", ch);
  }

  return 0;
}

Output for the different test-cases:-

Enter a character: K
K is not a vowel.

Enter a character: a
a is vowel.

If any character either in upper case or lower case is ‘a’, ‘e’, ‘i’, ‘o’, ‘u’ then the character is vowel otherwise it may be constant or space or special word. In this program, we have written a switch case. The case label becomes true if the character is a vowel. When the match is not found then the default statement will be executed.

Menu-driven program to find the area of a circle, triangle, and rectangle

Program:- Write a menu-driven C program to find the area of the circle, area of the triangle and area of the rectangle according to the user’s input choice.

The area is given by,
circle = ℼ*r2
triangle = 0.5*base*height
rectangle = length*breadth

#include<stdio.h>
int main()
{
  float r, area, base, height, l, b;
  int choice;

  printf("1. Area of a circle\n");
  printf("2. Area of a triangle\n");
  printf("3. Area of a rectangle\n");
  printf("Enter your choice: ");
  scanf("%d", &choice);

  switch(choice)
  {
    case 1:
      printf("Enter the radius of the circle: ");
      scanf("%f",&r);
      area = 3.14 * r * r;
      printf("Area of circle = %.2f",area);
      break;

    case 2:
      printf("Enter the base and height of the triangle: ");
      scanf("%f %f",&base, &height);
      area = 0.5 * base * height;
      printf("Area of triangle = %.2f",area);
      break;

    case 3:
      printf("Enter the length and breadth of the rectangle: ");
      scanf("%f %f",&l, &b);
      area = l*b;
      printf("Area of circle = %.2f",area);
      break;

    default:
      printf("Invalid choice.");
  }

  return 0;
}

Output for the different test-cases:-

1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 1
Enter the radius of the circle: 5
Area of circle = 78.50

1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 2
Enter the base and height of the triangle: 10 25
Area of triangle = 125.00

1. Area of a circle
2. Area of a triangle
3. Area of a rectangle
Enter your choice: 3
Enter the length and breadth of the rectangle: 8 6
Area of circle = 48.00

Menu-driven program to convert year

Program:- Write a menu-driven C program to convert a given year into,

  1. Months
  2. Days
  3. Hours
  4. Minutes
  5. Seconds

For simplicity don’t include leap year, use
1 Year = 365 days
1 Month = 30 days

#include<stdio.h>
int main()
{
  float year;
  double months,days,hours,minutes,seconds;
  int choice;

  printf("Enter year: ");
  scanf("%f",&year);

  printf("Convert year into,\n");
  printf("1. Months\n");
  printf("2. Days\n");
  printf("3. Hours\n");
  printf("4. Minutes\n");
  printf("5. Seconds\n");
  printf("Enter your choice: ");
  scanf("%d",&choice);

  switch(choice)
  {
    case 1:
      months = year * 12;
      printf("Months in %.2f years = %.2lf",year,months);
      break;

    case 2:
      days = year * 365;
      printf("Days in %.2f years = %.2lf",year,days);
      break;

    case 3:
      hours = year * 365 * 24;
      printf("Hours in %.2f years = %.2lf",year,hours);
      break;

    case 4:
      minutes = year * 365 * 24 * 60;
      printf("Minutes in %.2f years = %.2lf",year,minutes);
      break;

    case 5:
      seconds = year * 365 * 24 * 60 * 60;
      printf("Seconds in %.2f years = %.2lf",year,seconds);
      break;

    default:
      printf("Invalid choice.");
  }

  return 0;
}

Output for the different test-cases:-

Enter year: 5
Convert year into,
1. Months
2. Days
3. Hours
4. Minutes
5. Seconds
Enter your choice: 1
Months in 5.00 years = 60.00

Enter year: 6
Convert year into,
1. Months
2. Days
3. Hours
4. Minutes
5. Seconds
Enter your choice: 2
Days in 6.00 years = 2190.00

Enter year: .5
Convert year into,
1. Months
2. Days
3. Hours
4. Minutes
5. Seconds
Enter your choice: 5
Seconds in 0.50 years = 15768000.00

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Flow Control
If-else Statement in C
Programs on if-else
Switch Case in C
Switch case Programs
Conditional Operator
While loop in C
Do-while loop in C
While vs do-while
For loop in C
Break keyword in C
Continue keyword in C
Break vs Exit in C
Goto keyword in C
☕️ Flow Control Programs
Largest among 3 Numbers
Find Grade of student
Find the absolute value
Check Vowel or Consonant
Leap Year Program
Simple calculator in C
Check Odd or Even 
Roots of Quadratic Equation
Find Reverse of Number
Factors of a number in C
Generate Multiplication table
Find Power of a Number
Find GCD and LCM
Find factorial of Number
Count Number of Digits
Sum of digits in Number
Sum of N Natural Numbers
Sum of Squares of Natural No.
Find Sum of Odd Numbers
Find the Sum of Series
Find Fibonacci series in C
Sum of the Fibonacci series
Sum until enters +ve numbers
Sum of max 10 no. & Skip -ve
☕️ C Conversion Programs
Celsius to Fahrenheit
Fahrenheit to Celsius
Decimal ↔ Binary
Decimal ↔ Octal
Octal ↔ Binary in C
☕️ Number Programs in C
Prime Number in C
Strong Number in C
Krishnamurthy Number
Neon Number in C
Palindrome number
Perfect Number in C
Armstrong Number
☕️ Pattern Programs in C
Pattern programs in C
Printing pattern using loops
Floyd’s triangle Program
Pascal Triangle Program
Pyramid Star Pattern in C
Diamond Pattern Programs
Half Diamond pattern in C
Print Diamond Pattern
Hollow Diamond Pattern
Diamond Pattern of Numbers

Leave a Reply