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.


Example:
Input week number: 1
Output: Monday

Required knowledge

Basic C programming, If else

Program to print day of week

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

#include <stdio.h>

int main()
{
    int week;

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


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

    return 0;
} 


Also enhance your skills by learning this program using switch case.


Output
Enter week number (1-7): 1
MONDAY

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

1 comment: