Switch case program in C | 5+ Examples

In my previous article Switch statement in C, we have already discussed what is switch case statement is and it’s working.

Here, we will learn “switch case program in c” with examples and a flow chart to understand the concepts clearly.

So let’s start without wasting any time and we will start with the basic program and then go to advanced ones.

If you are here for the first time and not aware of the syntax of the switch case, please click here.

You can execute your code’s here with different values so see the different outputs.

Switch case program in C

Switch case program in C

1. C program to input number of week’s day(1-7) and translate to its equivalent name of the day of the week (e.g., 1 to Sunday, 2 to Monday…………….., 7 to Saturday)

#include<stdio.h>


void main()
{
    int ch;
   
    printf(“Enter number of week day(1-7):”);
    scanf("%d", &ch);

    switch(ch)
    {
        case 1: printf("\nSunday");
        break;
        case 2: printf("\nMonday");
        break;
        case 3: printf("\nTuesday");
        break;
        case 4: printf("\nWednesday");
        break;
        case 5: printf("\nThursday");
        break;
        case 6: printf("\nFriday");
        break;
        case 7: printf("\nSaturday");
        break;
        default: printf("\n Please try again and enter number between 1-7");
    }
return 0;
}

Output

Enter number of week day(1-7):1
Sunday

2. WAP to input a month & print its month name.

#include <stdio.h>
void main()
{
   int monno;
   printf("Input Month No : ");
   scanf("%d",&monno);
   switch(monno)
   {
	case 1:
	       printf("January\n");
	       break;
	case 2:
	       printf("February\n");
	       break;
	case 3:
	       printf("March\n");
	       break;
	case 4:
	       printf("April\n");
	       break;
	case 5:
	       printf("May\n");
	       break;
	case 6:
	       printf("June\n");
	       break;
	case 7:
	       printf("July\n");
	       break;
	case 8:
	       printf("August\n");
	       break;
	case 9:
	       printf("September\n");
	       break;
	case 10:
	       printf("October\n");
	       break;
	case 11:
	       printf("November\n");
	       break;
	case 12:
	       printf("December\n");
	       break;
	default:
	       printf("invalid Month number. \nPlease try again ....\n");
	       break;
      }
}

Output

Input Month No : 5
May

3. C Program to Write a menu-driven program using Switch case to calculate the following:

  1. Area of circle
  2. Area of square
  3. Area of sphere
#include <stdio.h>
int input();
void output(float);
int main()
{
	float result;
	int choice, num;
	printf("Press 1 to calculate area of circle\n");
	printf("Press 2 to calculate area of square\n");
	printf("Press 3 to calculate area of sphere\n");
	printf("Enter your choice:\n");
	choice = input();
	
	switch (choice) {
	case 1: {
		printf("Enter radius:\n");
		num = input();
		result = 3.14 * num * num;
		printf("Area of sphere=");
		output(result);
		break;
	}
	case 2: {
		printf("Enter side of square:\n");
		num = input();
		result = num * num;
		printf("Area of square=");
		output(result);
		break;
	}
	case 3: {
		printf("Enter radius:\n");
		num = input();
		result = 4 * (3.14 * num * num);
		printf("Area of sphere=");
		output(result);
		break;
	}
	default:
		printf("wrong Input\n");
	}
	return 0;
}
int input()
{
	int number;
	scanf("%d", &number);
	return (number);
}

void output(float number)
{
	printf("%f", number);
}

Output

Press 1 to calculate area of circle
Press 2 to calculate area of square
Press 3 to calculate area of sphere
Enter your choice:
2
Enter side of square:
5
Area of square=25.0000005

4. Write a Menu Driven Program to find

  1. Factorial of a number
  2. Prime Number
  3. Find Number is odd or even
#include<stdio.h>

int main()
{
    int choice, num, i;
    unsigned long int fact;

    
        printf("1. Factorial \n");
        printf("2. Prime\n");
        printf("3. Odd\\Even\n");
        printf("4. Exit\n\n\n");
        printf("Enter your choice :  ");
        scanf("%d",&choice);
        
        switch(choice)
        {
            case 1:
                printf("Enter number:\n");
                scanf("%d", &num);
                fact = 1;
                for(i = 1; i <= num; i++)
                {
                    fact = fact*i;
                }
                printf("\n\nFactorial value of %d is = %lu\n\n\n",num,fact);
                break;
        
            case 2:
                printf("Enter number:\n");
                scanf("%d", &num);
                if(num == 1)
                printf("\n1 is neither prime nor composite\n\n");
                for(i = 2; i < num; i++)
                {
                    if(num%i == 0)
                    {
                        printf("\n%d is not a prime number\n\n", num);
                        break;
                    }
                
                }
                /*
                    Not divisible by any number other 
                    than 1 and itself
                */
                if(i == num) 
                {
                    printf("\n\n%d is a Prime number\n\n", num);
                    break;
                }
        
            case 3:
                printf("Enter number:\n");
                scanf("%d", &num);
        
                if(num%2 == 0) // 0 is considered to be an even number
                    printf("\n\n%d is an Even number\n\n",num);
                else
                    printf("\n\n%d is an Odd number\n\n",num);
                break;
        
            case 4:
                exit(0);    // terminates the complete program execution
     
    }
    return 0;
}

OutPut

1. Factorial 
2. Prime
3. Odd\Even
4. Exit


Enter your choice :  1
Enter number:
5
Factorial value of 5 is = 120


1. Factorial 
2. Prime
3. Odd\Even
4. Exit


Enter your choice :  2
Enter number:
29
29 is a Prime number

1. Factorial 
2. Prime
3. Odd\Even
4. Exit

5. Write a menu-driven program using a function which can do the following operations:

  1. Addition of two numbers
  2. Subtraction of two numbers
  3. multiplication of two numbers
  4. division of two numbers
#include<stdio.h>
void main()
{
int a,b,ch;
char choice;
printf("\n Enter two numbers:");
scanf("%d%d",&a,&b);
do{
printf("\n Press 1 to add two numbers ");
printf("\n Press 2 to subtract two numbers ");
printf("\n Press 3 to multiply two numbers ");
printf("\n Press 4 to divide two numbers \n");
scanf("%d",&ch);
switch(ch)
{
case 1: printf("Sum: %d",a+b);
 		break;
case 2: printf("Subtract :%d",a-b);
		break;
case 3: printf("Multiply :%d",a*b);
 		break;
case 4: if(b==0)
		printf("\n Denominator cannot be zero");
        else
		printf("Divide :%d",a/b); // b should not be zero
		break;
default:printf("Wrong choice!");
}
printf("\n Do you want to continue? (Press y/n)");
scanf(" %c",&choice);
}while(choice=='y');
}

Output

 Enter two numbers:5
 4
 Press 1 to add two numbers 
 Press 2 to subtract two numbers 
 Press 3 to multiply two numbers 
 Press 4 to divide two numbers 
1
Sum: 9
 Do you want to continue? (Press y/n)y
 Press 1 to add two numbers 
 Press 2 to subtract two numbers 
 Press 3 to multiply two numbers 
 Press 4 to divide two numbers 
2
Subtract :1
 Do you want to continue? (Press y/n)n

6. Program:- Write a menu-driven C program to convert a given year into Months, Days, Hours, Minutes and 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

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

You Might Like:

Leave a Comment

Ads Blocker Image Powered by Code Help Pro
Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Please Reload After Disabling Ad\'s