C program to enter week number and print day of week

Write a C program to enter week number(1-7) and print the corresponding day of week name using if else. How to print day of week using if else in C programming. Program to enter week number from user and print day of week name using if else in C programming. Logic to convert week number to day of week in C program.

Example
Input

Input week number: 1

Output

Monday

Required knowledge

Basic C programming, If else

Logic to find day of week

Converting week day number to day of week name is simple and easy. Below is the step by step descriptive logic to print day of week from given week day number.

  1. Input week day number from user and store it in some variable say week.
  2. Print Monday if week == 1. Assuming that first day of week is Monday.
  3. Like above step check all 7 day numbers and print the corresponding day name.

Program to print day name of week

/**
 * C program to print day of week
 */

#include <stdio.h>

int main()
{
    int week;

    /* Input week number from user */
    printf("Enter week number (1-7): ");
    scanf("%d", &week);


    if(week == 1)
    {
        printf("MONDAY");
    }
    else if(week == 2)
    {
        printf("TUESDAY");
    }
    else if(week == 3)
    {
        printf("WEDNESDAY");
    }
    else if(week == 4)
    {
        printf("THURSDAY");
    }
    else if(week == 5)
    {
        printf("FRIDAY");
    }
    else if(week == 6)
    {
        printf("SATURDAY");
    }
    else if(week == 7)
    {
        printf("SUNDAY");
    }
    else
    {
        printf("Invalid Input! Please enter week number between 1-7.");
    }

    return 0;
}

If else is easiest method to code this program. However, switch statements are recommended for these problems.

Read more - Program to print day name of week using switch case

However, using an array you can easily cut length of the program. Below program illustrate how to print day of week using array.

Program to print day name of week using array constant

/**
 * C program to print day of week
 */

#include <stdio.h>

int main()
{
    // Declares a constant name of weeks
    const char *WEEKS[] = { "Monday", "Tuesday", "Wednesday", 
                            "Thursday", "Friday", "Saturday", 
                            "Sunday"};
    int week;

    /* Input week number from user */
    printf("Enter week number (1-7): ");
    scanf("%d", &week);
	
    if(week > 0 && week < 8)
    {
        printf("%s", WEEKS[week-1]);
    }
    else
    {
        printf("Invalid input! Please enter week number between 1-7.");
    }

    return 0;
}

Output

Enter week number (1-7): 1
MONDAY

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>