w3resource

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:

Accept day number and display its equivalent day name in the word


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:

Flowchart: Accept day number and display its equivalent day name in word


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.

Go to:


PREV : Grade to Description Mapping.
NEXT : Digit to Word Conversion.

C Programming Code Editor:



Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.