C Exercises: Accept day number and display its equivalent day name in the word
21. Day Number to Day Name
Write a C program to read any day number in integer and display the day name in word format.
Visual Presentation:
Sample Solution:
C Code:
#include <stdio.h> // Include the standard input/output header file.
void main()
{
int dayno; // Declare a variable to store the day number.
printf("Input Day No : "); // Prompt the user to input the day number.
scanf("%d",&dayno); // Read and store the day number.
switch(dayno) // Start a switch statement based on the day number.
{
case 1:
printf("Monday \n"); // Print the corresponding day for day number 1.
break;
case 2:
printf("Tuesday \n"); // Print the corresponding day for day number 2.
break;
case 3:
printf("Wednesday \n"); // Print the corresponding day for day number 3.
break;
case 4:
printf("Thursday \n"); // Print the corresponding day for day number 4.
break;
case 5:
printf("Friday \n"); // Print the corresponding day for day number 5.
break;
case 6:
printf("Saturday \n"); // Print the corresponding day for day number 6.
break;
case 7:
printf("Sunday \n"); // Print the corresponding day for day number 7.
break;
default:
printf("Invalid day number. \nPlease try again ....\n"); // Print a message for invalid day number.
break;
}
}
Output:
Input Day No : 4 Thursday
Flowchart:
For more Practice: Solve these Related Problems:
- Write a C program to convert a day number to its name and handle invalid numbers gracefully.
- Write a C program to display the day name for a given day number using an array of strings.
- Write a C program to map an integer to a day of the week and output a special message for weekends.
- Write a C program to convert a day number to its corresponding day name using a switch-case construct with input validation.
C Programming Code Editor:
Previous: Write a program in C to accept a grade and display the equivalent description
Next: Write a program in C to read any digit, display in the word.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.