C Programming • C Basics, Operators, Loops
C Programming / switch-case — Exercises

switch-case — Exercises

Practical 2 C Basics, Operators, Loops

Write a C program to display the day of the week using a number from 1 to 7 entered by the user.

Practical / Solution

switch-case — Exercise 1

Problem

Write a C program to display the day of the week using a number from 1 to 7 entered by the user.

Program

#include <stdio.h> int main() { int day; printf("Enter day number (1-7): "); scanf("%d", &day); // Select the day using switch switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; case 4: printf("Thursday\n"); break; case 5: printf("Friday\n"); break; case 6: printf("Saturday\n"); break; case 7: printf("Sunday\n"); break; default: printf("Invalid day number.\n"); } return 0; }

Explanation

The switch statement compares the value of day with each case. The matching case is executed and break prevents execution from continuing into the next case.

Expected Output

Enter day number (1-7): 3

Wednesday

switch-case — Exercise 2

Problem

Write a C program using switch-case to display the name of a month based on a number from 1 to 12.

Program

#include <stdio.h> int main() { int month; printf("Enter month number (1-12): "); scanf("%d", &month); // Select month name switch (month) { 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.\n"); } return 0; }

Explanation

Each month number is associated with a separate case. The default case handles invalid input.

Expected Output

Enter month number (1-12): 8

August

switch-case — Exercise 3

Problem

Write a simple calculator using switch-case for addition, subtraction, multiplication and division.

Program

#include <stdio.h> int main() { float a, b; char op; printf("Enter first number: "); scanf("%f", &a); printf("Enter operator (+, -, *, /): "); scanf(" %c", &op); printf("Enter second number: "); scanf("%f", &b); // Select operation using switch switch (op) { case '+': printf("Result = %.2f\n", a + b); break; case '-': printf("Result = %.2f\n", a - b); break; case '*': printf("Result = %.2f\n", a * b); break; case '/': if (b != 0) printf("Result = %.2f\n", a / b); else printf("Division by zero is not allowed.\n"); break; default: printf("Invalid operator.\n"); } return 0; }

Explanation

The operator entered by the user determines which case is executed. Division also checks for zero before performing the operation.

Expected Output

Enter first number: 20

Enter operator (+, -, *, /): *

Enter second number: 5

Result = 100.00

switch-case — Exercise 4

Problem

Write a C program using switch-case to determine whether a character is a vowel.

Program

#include <stdio.h> int main() { char ch; printf("Enter a character: "); scanf(" %c", &ch); // Check vowel using switch switch (ch) { case 'a': case 'e': case 'i': case 'o': case 'u': case 'A': case 'E': case 'I': case 'O': case 'U': printf("The character is a vowel.\n"); break; default: printf("The character is not a vowel.\n"); } return 0; }

Explanation

Multiple case labels can perform the same operation. Here, all vowel characters share a common statement.

Expected Output

Enter a character: E

The character is a vowel.

switch-case — Exercise 5

Problem

Write a C program to display the type of traffic signal based on the entered character: R for Red, Y for Yellow and G for Green.

Program

#include <stdio.h> int main() { char signal; printf("Enter signal (R/Y/G): "); scanf(" %c", &signal); // Identify traffic signal switch (signal) { case 'R': case 'r': printf("Stop\n"); break; case 'Y': case 'y': printf("Wait\n"); break; case 'G': case 'g': printf("Go\n"); break; default: printf("Invalid signal.\n"); } return 0; }

Explanation

The switch statement maps each signal character to its corresponding instruction.

Expected Output

Enter signal (R/Y/G): G

Go

switch-case — Exercise 6

Problem

Write a C program using switch-case to calculate the area of a selected shape: circle, rectangle or square.

Program

#include <stdio.h> int main() { int choice; float radius, length, width, side; printf("1. Circle\n"); printf("2. Rectangle\n"); printf("3. Square\n"); printf("Enter choice: "); scanf("%d", &choice); // Select shape switch (choice) { case 1: printf("Enter radius: "); scanf("%f", &radius); printf("Area = %.2f\n", 3.14159 * radius * radius); break; case 2: printf("Enter length and width: "); scanf("%f %f", &length, &width); printf("Area = %.2f\n", length * width); break; case 3: printf("Enter side: "); scanf("%f", &side); printf("Area = %.2f\n", side * side); break; default: printf("Invalid choice.\n"); } return 0; }

Explanation

The value of choice determines which shape calculation is performed.

Expected Output

1. Circle

2. Rectangle

3. Square

Enter choice: 2

Enter length and width: 10 5

Area = 50.00

switch-case — Exercise 7

Problem

Write a C program using switch-case to display the number of days in a month for a given month number.

Program

#include <stdio.h> int main() { int month; printf("Enter month number (1-12): "); scanf("%d", &month); // Group months having the same number of days switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: printf("31 days.\n"); break; case 4: case 6: case 9: case 11: printf("30 days.\n"); break; case 2: printf("28 or 29 days.\n"); break; default: printf("Invalid month number.\n"); } return 0; }

Explanation

Multiple case labels can be grouped when they produce the same output. This avoids writing the same statement repeatedly.

Expected Output

Enter month number (1-12): 4

30 days.

switch-case — Exercise 8

Problem

Write a C program to display the grade description based on a grade character entered by the user: A, B, C, D or F.

Program

#include <stdio.h> int main() { char grade; printf("Enter grade: "); scanf(" %c", &grade); // Display grade description switch (grade) { case 'A': case 'a': printf("Excellent performance.\n"); break; case 'B': case 'b': printf("Very good performance.\n"); break; case 'C': case 'c': printf("Good performance.\n"); break; case 'D': case 'd': printf("Needs improvement.\n"); break; case 'F': case 'f': printf("Fail.\n"); break; default: printf("Invalid grade.\n"); } return 0; }

Explanation

Each grade character is mapped to a corresponding description using switch-case.

Expected Output

Enter grade: B

Very good performance.

switch-case — Exercise 9

Problem

Write a menu-driven C program using switch-case to perform addition, subtraction, multiplication and division based on the user's choice.

Program

#include <stdio.h> int main() { int choice; float a, b; printf("1. Addition\n"); printf("2. Subtraction\n"); printf("3. Multiplication\n"); printf("4. Division\n"); printf("Enter choice: "); scanf("%d", &choice); printf("Enter two numbers: "); scanf("%f %f", &a, &b); // Perform selected calculation switch (choice) { case 1: printf("Result = %.2f\n", a + b); break; case 2: printf("Result = %.2f\n", a - b); break; case 3: printf("Result = %.2f\n", a * b); break; case 4: if (b != 0) printf("Result = %.2f\n", a / b); else printf("Division by zero is not allowed.\n"); break; default: printf("Invalid choice.\n"); } return 0; }

Explanation

The menu gives the user multiple choices, and the selected case performs the corresponding arithmetic operation.

Expected Output

1. Addition

2. Subtraction

3. Multiplication

4. Division

Enter choice: 1

Enter two numbers: 25 15

Result = 40.00

switch-case — Exercise 10

Problem

Write a C program using switch-case to perform a simple unit conversion: 1 for kilometres to metres, 2 for metres to centimetres, and 3 for kilograms to grams.

Program

#include <stdio.h> int main() { int choice; float value; printf("1. Kilometres to Metres\n"); printf("2. Metres to Centimetres\n"); printf("3. Kilograms to Grams\n"); printf("Enter choice: "); scanf("%d", &choice); printf("Enter value: "); scanf("%f", &value); // Perform selected conversion switch (choice) { case 1: printf("Result = %.2f metres\n", value * 1000); break; case 2: printf("Result = %.2f centimetres\n", value * 100); break; case 3: printf("Result = %.2f grams\n", value * 1000); break; default: printf("Invalid choice.\n"); } return 0; }

Explanation

The switch statement selects one conversion formula according to the user's choice.

Expected Output

1. Kilometres to Metres

2. Metres to Centimetres

3. Kilograms to Grams

Enter choice: 1

Enter value: 2.5

Result = 2500.00 metres