0
0
Cprogramming~15 mins

Switch statement - Mini Project: Build & Apply

Choose your learning style9 modes available
Using a Switch Statement in C
📖 Scenario: You are creating a simple program that tells the name of a day based on a number input from 1 to 7.
🎯 Goal: Build a C program that uses a switch statement to print the correct day name for a given number.
📋 What You'll Learn
Create an integer variable called dayNumber with the value 3
Create an integer variable called invalidDay with the value 0
Use a switch statement on dayNumber with cases for numbers 1 to 7
Print the correct day name for each case
Use default case to print "Invalid day" if the number is not 1 to 7
Print the result using printf
💡 Why This Matters
🌍 Real World
Switch statements are used in many programs to choose actions based on a value, like menu selections or commands.
💼 Career
Understanding switch statements helps in writing clear and efficient code for decision-making in software development.
Progress0 / 4 steps
1
Create the day number variable
Create an integer variable called dayNumber and set it to 3.
C
Need a hint?

Use int dayNumber = 3; to create the variable.

2
Create the invalid day variable
Create an integer variable called invalidDay and set it to 0.
C
Need a hint?

Use int invalidDay = 0; to create the variable.

3
Write the switch statement for day names
Write a switch statement using dayNumber with cases 1 to 7. For each case, use printf to print the day name: 1 for "Monday", 2 for "Tuesday", 3 for "Wednesday", 4 for "Thursday", 5 for "Friday", 6 for "Saturday", 7 for "Sunday". Add a default case that prints "Invalid day".
C
Need a hint?

Use switch(dayNumber) { ... } with case labels and break; after each printf.

4
Print the day name result
Run the program to print the day name for dayNumber. The output should be Wednesday.
C
Need a hint?

Compile and run the program. It should print Wednesday.