switch Statement in C Practice Questions with Solutions

Introduction

The switch statement in C is used when a program needs to choose one option from multiple fixed choices. It is commonly used for menus, calculator operations, days, months, and simple command-based programs. In this chapter, you will practice switch, case, break, and default through 10 beginner-friendly examples. The questions gradually move from simple choices to practical programs so you can understand how switch works before using it in larger C programs. switch Statement in C practice questions with solutions to help you understand the concepts.

Q1. Display a Day Using switch in C

Problem Statement

Write a C program that accepts a number from 1 to 7 and displays the corresponding day.

  • 1 → Monday
  • 2 → Tuesday
  • 3 → Wednesday
  • 4 → Thursday
  • 5 → Friday
  • 6 → Saturday
  • 7 → Sunday

C Program

#include <stdio.h>

int main()
{
    int day;

    printf("Enter day number (1-7): ");
    scanf("%d", &day);

    switch (day)
    {
        case 1:
            printf("Monday");
            break;

        case 2:
            printf("Tuesday");
            break;

        case 3:
            printf("Wednesday");
            break;

        case 4:
            printf("Thursday");
            break;

        case 5:
            printf("Friday");
            break;

        case 6:
            printf("Saturday");
            break;

        case 7:
            printf("Sunday");
            break;

        default:
            printf("Invalid day number.");
    }

    return 0;
}

Sample Output

Enter day number (1-7): 3
Wednesday

Explanation

The value of day is compared with every case.

For example:

day = 3

The program finds:

case 3:

and executes:

printf("Wednesday");

The break statement then exits the switch.

Concepts Covered

  • switch
  • case
  • break
  • default
  • Integer selection

Q2. Display a Month Using switch

Problem Statement

Write a C program that accepts a number from 1 to 12 and displays the corresponding month.

C Program

#include <stdio.h>

int main()
{
    int month;

    printf("Enter month number (1-12): ");
    scanf("%d", &month);

    switch (month)
    {
        case 1:
            printf("January");
            break;

        case 2:
            printf("February");
            break;

        case 3:
            printf("March");
            break;

        case 4:
            printf("April");
            break;

        case 5:
            printf("May");
            break;

        case 6:
            printf("June");
            break;

        case 7:
            printf("July");
            break;

        case 8:
            printf("August");
            break;

        case 9:
            printf("September");
            break;

        case 10:
            printf("October");
            break;

        case 11:
            printf("November");
            break;

        case 12:
            printf("December");
            break;

        default:
            printf("Invalid month number.");
    }

    return 0;
}

Sample Output

Enter month number (1-12): 8
August

Explanation

The program compares month with values from 1 to 12.

If the user enters 8, the following case executes:

case 8:
    printf("August");
    break;

Concepts Covered

  • Multiple case statements
  • switch
  • break
  • default

Q3. Create a Simple Calculator Using switch in C

Problem Statement

Write a C program that accepts two numbers and an operator.

The program should perform:

  • + Addition
  • - Subtraction
  • * Multiplication
  • / Division

C Program

#include <stdio.h>

int main()
{
    float a, b;
    char operator;

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

    printf("Enter operator (+, -, *, /): ");
    scanf(" %c", &operator);

    printf("Enter second number: ");
    scanf("%f", &b);

    switch (operator)
    {
        case '+':
            printf("Result = %.2f", a + b);
            break;

        case '-':
            printf("Result = %.2f", a - b);
            break;

        case '*':
            printf("Result = %.2f", a * b);
            break;

        case '/':
            if (b != 0)
            {
                printf("Result = %.2f", a / b);
            }
            else
            {
                printf("Cannot divide by zero.");
            }
            break;

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

    return 0;
}

Sample Output

Enter first number: 20
Enter operator (+, -, *, /): *
Enter second number: 5
Result = 100.00

Explanation

Here, the switch expression is a character:

switch (operator)

If the user enters *, the following case is selected:

case '*':
    printf("Result = %.2f", a * b);
    break;

The if statement inside the division case prevents division by zero.

Concepts Covered

  • Character switch
  • Arithmetic operators
  • case
  • break
  • Nested if
  • Division by zero

Q4. Check Vowel Using switch in C

Problem Statement

Write a C program that accepts a character and checks whether it is a vowel.

Consider:

a, e, i, o, u

and their uppercase versions as vowels.

C Program

#include <stdio.h>

int main()
{
    char ch;

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

    switch (ch)
    {
        case 'a':
        case 'A':
        case 'e':
        case 'E':
        case 'i':
        case 'I':
        case 'o':
        case 'O':
        case 'u':
        case 'U':
            printf("It is a vowel.");
            break;

        default:
            printf("It is not a vowel.");
    }

    return 0;
}

Sample Output

Enter a character: E
It is a vowel.

Explanation

Notice that several case labels are placed together:

case 'a':
case 'A':
case 'e':
case 'E':

They all lead to the same statement.

This is called fall-through between cases and is useful when multiple values should perform the same action.

Concepts Covered

  • Character switch
  • Multiple cases
  • Fall-through
  • Character comparison

Q5. Display a Menu Using switch

Problem Statement

Create a simple menu-driven C program.

Display:

1. Add
2. Subtract
3. Multiply
4. Divide

Ask the user to choose an option and display the selected operation.

C Program

#include <stdio.h>

int main()
{
    int choice;

    printf("===== MENU =====\n");
    printf("1. Add\n");
    printf("2. Subtract\n");
    printf("3. Multiply\n");
    printf("4. Divide\n");

    printf("Enter your choice: ");
    scanf("%d", &choice);

    switch (choice)
    {
        case 1:
            printf("You selected Addition.");
            break;

        case 2:
            printf("You selected Subtraction.");
            break;

        case 3:
            printf("You selected Multiplication.");
            break;

        case 4:
            printf("You selected Division.");
            break;

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

    return 0;
}

Sample Output

===== MENU =====
1. Add
2. Subtract
3. Multiply
4. Divide
Enter your choice: 3
You selected Multiplication.

Explanation

The user’s choice is stored in:

int choice;

The switch then selects the corresponding menu option.

Concepts Covered

  • Menu-driven program
  • switch
  • case
  • default
  • User input

Q6. Find the Number of Days in a Month

Problem Statement

Write a C program that accepts a month number and displays the number of days in that month.

For this basic program, consider February as having 28 days.

C Program

#include <stdio.h>

int main()
{
    int month;

    printf("Enter month number (1-12): ");
    scanf("%d", &month);

    switch (month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            printf("This month has 31 days.");
            break;

        case 4:
        case 6:
        case 9:
        case 11:
            printf("This month has 30 days.");
            break;

        case 2:
            printf("This month has 28 days.");
            break;

        default:
            printf("Invalid month number.");
    }

    return 0;
}

Sample Output

Enter month number (1-12): 4
This month has 30 days.

Explanation

Several months have the same number of days, so they can share the same statements.

For example:

case 4:
case 6:
case 9:
case 11:
    printf("This month has 30 days.");
    break;

Concepts Covered

  • Multiple cases
  • Case grouping
  • break
  • Menu-style selection
  • Practical use of switch

Q7. Check Even or Odd Using switch

Problem Statement

Write a C program that checks whether a number is even or odd using switch.

C Program

#include <stdio.h>

int main()
{
    int number;

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

    switch (number % 2)
    {
        case 0:
            printf("The number is even.");
            break;

        case 1:
        case -1:
            printf("The number is odd.");
            break;
    }

    return 0;
}

Sample Output

Enter a number: 25
The number is odd.

Explanation

The expression:

number % 2

produces:

  • 0 for an even number
  • 1 or -1 for an odd number, depending on the sign of the number

The switch then selects the appropriate case.

Concepts Covered

  • Expression in switch
  • Modulus operator
  • Multiple cases
  • Even/odd checking

Q8. Convert a Number to a Day Type

Problem Statement

Write a C program that accepts a day number from 1 to 7 and displays whether it is a weekday or weekend.

  • 1–5 → Weekday
  • 6–7 → Weekend

C Program

#include <stdio.h>

int main()
{
    int day;

    printf("Enter day number (1-7): ");
    scanf("%d", &day);

    switch (day)
    {
        case 1:
        case 2:
        case 3:
        case 4:
        case 5:
            printf("Weekday");
            break;

        case 6:
        case 7:
            printf("Weekend");
            break;

        default:
            printf("Invalid day number.");
    }

    return 0;
}

Sample Output

Enter day number (1-7): 6
Weekend

Explanation

Days 1 through 5 share the same output, so they are grouped together.

Days 6 and 7 also share the same output.

Concepts Covered

  • Grouped cases
  • switch
  • break
  • default
  • Multiple choices

Q9. Perform a Simple Unit Conversion

Problem Statement

Create a C program with the following menu:

1. Kilometers to Meters
2. Meters to Centimeters
3. Kilograms to Grams

Ask the user to select an option and enter a value.

C Program

#include <stdio.h>

int main()
{
    int choice;
    float value;

    printf("===== UNIT CONVERTER =====\n");
    printf("1. Kilometers to Meters\n");
    printf("2. Meters to Centimeters\n");
    printf("3. Kilograms to Grams\n");

    printf("Enter your choice: ");
    scanf("%d", &choice);

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

    switch (choice)
    {
        case 1:
            printf("Result = %.2f meters", value * 1000);
            break;

        case 2:
            printf("Result = %.2f centimeters", value * 100);
            break;

        case 3:
            printf("Result = %.2f grams", value * 1000);
            break;

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

    return 0;
}

Sample Output

===== UNIT CONVERTER =====
1. Kilometers to Meters
2. Meters to Centimeters
3. Kilograms to Grams
Enter your choice: 1
Enter value: 5
Result = 5000.00 meters

Explanation

The choice determines which conversion formula is used.

For example:

choice = 1
value = 5

The program executes:

value * 1000

and displays 5000 meters.

Concepts Covered

  • Menu-driven program
  • switch
  • Arithmetic calculations
  • float
  • User input

Q10. Build a Menu-Driven Calculator

Problem Statement

Create a menu-driven calculator using switch.

The menu should contain:

1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder

Accept two integers and perform the selected operation.

C Program

#include <stdio.h>

int main()
{
    int choice;
    int a, b;

    printf("===== CALCULATOR =====\n");
    printf("1. Addition\n");
    printf("2. Subtraction\n");
    printf("3. Multiplication\n");
    printf("4. Division\n");
    printf("5. Remainder\n");

    printf("Enter your choice: ");
    scanf("%d", &choice);

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

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

    switch (choice)
    {
        case 1:
            printf("Result = %d", a + b);
            break;

        case 2:
            printf("Result = %d", a - b);
            break;

        case 3:
            printf("Result = %d", a * b);
            break;

        case 4:
            if (b != 0)
            {
                printf("Result = %d", a / b);
            }
            else
            {
                printf("Cannot divide by zero.");
            }
            break;

        case 5:
            if (b != 0)
            {
                printf("Remainder = %d", a % b);
            }
            else
            {
                printf("Cannot calculate remainder with zero.");
            }
            break;

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

    return 0;
}

Sample Output

===== CALCULATOR =====
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Remainder
Enter your choice: 5
Enter first number: 25
Enter second number: 4
Remainder = 1

Explanation

The choice determines which operation is performed.

For example:

choice = 5

selects:

case 5:

and calculates:

a % b

The program also checks whether b is zero before division or remainder operations.

Concepts Covered

  • switch
  • Multiple case statements
  • break
  • default
  • Nested if
  • Arithmetic operators
  • Modulus
  • Menu-driven programming

Key Takeaways

  • switch is used to select one execution path from multiple fixed choices.
  • case defines the possible values.
  • break normally exits the switch after a matching case.
  • default handles values that do not match any case.
  • Multiple cases can share the same code.
  • switch can use an expression as its controlling value.
  • Character values can also be used in switch.
  • Fall-through happens when execution reaches another case without encountering a break.
  • switch is especially useful for menu-driven programs.
  • if-else is generally more suitable when conditions involve ranges such as marks >= 80.
  • A switch case value must be an integer constant expression; arbitrary runtime expressions cannot be used as case labels.
  • switch cases are compared using the value of the controlling expression after the relevant integer promotions.

FAQs

1. What is a switch statement in C?

A switch statement allows a program to choose between multiple fixed cases based on the value of an expression.

2. What is the purpose of case in C?

A case defines a possible value of the switch expression.

Example:

switch (choice)
{
    case 1:
        printf("Add");
        break;
}

If choice is 1, the case matches.

3. Why is break used in a switch statement?

break stops execution of the current switch and transfers control to the statement after the switch.

Without it, execution can continue into subsequent cases.

4. Is break mandatory in every case?

No. It is not mandatory.

Sometimes intentional fall-through is useful:

case 1:
case 2:
case 3:
    printf("Valid option");
    break;

Here, cases 1, 2, and 3 intentionally share the same code.

5. What is the default case?

default executes when the value of the switch expression does not match any case.

Example:

default:
    printf("Invalid choice.");

6. Can we use a character in a switch statement?

Yes. Character constants can be used as case values.

char grade = 'A';

switch (grade)
{
    case 'A':
        printf("Excellent");
        break;
}

7. Can we use conditions such as marks >= 80 directly as a case?

No. case labels are not general Boolean conditions.

For range-based conditions such as:

marks >= 80

an if-else structure is generally appropriate.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top