C Program to Print Day Name of Week

How to write a C Program to Print Day Name of Week using Else If statement, and Switch Condition with examples.

C Program to Print Day Name of Week using Else If

This C program will ask the user to enter any number between 1 to 7, where 1 is Monday, 2 is Tuesday, 3 = Wednesday, 4 = Thursday, 5 = Friday, 6 = Saturday, and 7 = Sunday. Based on the user entered integer value this program will print the day name.

To accomplish this goal, we are using C Else If Statement. But, we strongly recommend the Switch Case approach, which we explained in the second example.

#include <stdio.h>
int main()
{
int weekday;
printf(" Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) : ");
scanf("%d", &weekday);

if (weekday == 1)
{
printf("\n Today is Monday");
}
else if ( weekday == 2 )
{
printf("\n Today is Tuesday");
}
else if ( weekday == 3 )
{
printf("\n Today is Wednesday");
}
else if ( weekday == 4 )
{
printf("\n Today is Thursday");
}
else if ( weekday == 5 )
{
printf("\n Today is Friday");
}
else if ( weekday == 6 )
{
printf("\n Today is Saturday");
}
else if ( weekday == 7 )
{
printf("\n Today is Sunday");
}
else
printf("\n Please enter Valid Number between 1 to 7");

return 0;
}
 Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) :  5

 Today is Friday

Let m enter the value other than 1 to 7 (something like 12)

 Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) :  12

 Please enter Valid Number between 1 to 7

C Program to return Day Name of Week using Switch Condition

It is an ideal C Programming approach to deal with multiple conditions. In this C program, we are using the C Switch Case approach.

#include <stdio.h>

int main()
{
  int weekday;
  printf(" Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) :  ");
  scanf("%d", &weekday);
  
  switch (weekday)
  {
  	case 1:
  		printf("\n Today is Monday");
		  break;
  	case 2:
  		printf("\n Today is Tuesday");
		  break;
  	case 3:
  		printf("\n Today is Wednesday"); 
		  break;
  	case 4:
  		printf("\n Today is Thursday"); 
		  break;
  	case 5:
  		printf("\n Today is Friday"); 
		  break;
  	case 6:
  		printf("\n Today is Saturday");
		  break;
  	case 7:
  		printf("\n Today is Sunday");
		  break;
	default:
		printf("\n Please enter Valid Number between 1 to 7");
  }
  return 0;
}
C Program to Print Day Name of Week using Switch Condition 3

Let me try the value outside 1 to 7 (25)

 Please Enter the Day Number 1 to 7 (Consider 1= Monday, and 7 = Sunday) :  25

 Please enter Valid Number between 1 to 7

Also Read