Enter week number and print day of week program in C Language

Day-of-week-program-in-c-language

Day of Week Program Description:

Write a day of week Program in C language, Which takes the week Number from the user and print the day of Week using the if else condition. Day of the week program will take an Integer from the user and display the week day. And Program should do the error checking and display the Error message if the user enters an Invalid number.

If the user enters 1, The Program should display the Week day as Sunday, for 2 it should display Monday, and so on. Here is the table for the same.

Day NumberDay of Week
1Sunday
2Monday
3Tuesday
4Wednesday
5Thursday
6Friday
7Saturday

Here are the excepted Input and output of the program

Excepted Input and Output:

Example1:

Input:

Enter day Number [1-7]: 4

Output:

Wednesday

Example1:

Input:

Enter day Number [1-7]: 6

Output:

Friday

Example1:

Input:

Enter day Number [1-7]: 9

Output:

ERROR: Invalid Input, Please provide valid input

Prerequisites:

We need to know the basics of the decision-making statements like the if..else statement, if else ladder, and switch case. please go through the following articles to learn more.

We can write the Day of week program in different ways, Let’s look at the following two methods

  • Method 1: Day of week using if else ladder
  • Method 2: Day of week using switch statement or switch case.

Method 1: Day of Week Program Explanation or Algorithm:

We will use the if else ladder along with the relational operators to check the day of week of the given number.

  • We take the week number from the user and store the week number to a variable day.
  • Then we start the if else ladder and we will compare the given week number i.e day with the numbers 1, 2, 3, … 7.
  • If the day is equal to 1, Then day of week is Sunday.
  • Similarly, If the day is equal to 2, Then the day of week is Monday, Like this we check the week number ( day) with all the numbers up to 7.
  • If the day doesn’t match any number from 1 to 7, Then User provided an Invalid Number. So at the final else statement, We are displaying the Error message saying – ERROR: Invalid Input, Please provide valid input

Program: Day of week program in C language:

Program Output:

We are using the GCC Compiler to compile and run the program. By GCC compiler names the executable file as the a.out. ( As I am not renaming the executable file during the compilation, we also get the executable file as a.out)

You can learn more about the C program compilation on Linux in the following article

Example 1:

Example 2:

More Examples:

Invalid Input Example:

As you can see if the user enters any other value other than the 1 to 7, The program will display the Error message.

Method 2: Day of Week Program using the Switch statement in C programming

The program logic is almost the same, Except in this case we are using the Switch case instead of the if else ladder.

📢 If the number of comparisons is increasing then, If else ladder can cause some confusion, So it is advised to use the Switch statement whenever you feel it appropriate.

Here is the Day of week program using the switch statement. This code looks more readable and easily maintainable.

We used the switch statement‘s Default Case to handle the Invalid Inputs.

Program Output:

As you can see we got the excepted results and also handles the Invalid Inputs.

C Tutorials Index:

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to check Day of week using Switch Case […]

  2. […] Enter the week number and print the day of week program Using Switch Statement […]

Leave a Reply