C Program to Display the Day Name According To a Number Entered

This C program displays the name of the day by asking the user to input a number. For example, if the user enters 1 then “SUNDAY” is displayed, if the user enters 2 then “MONDAY” is displayed and so on. The C program code to display the day name according to number you entered are as follows.

#include<stdio.h>

int main()

{
    int n;

    printf("Enter a number from 1 to 7 \n");

    scanf("%d", &n);

    if (n<=7)

    {

        if (n==1)

            printf ("SUNDAY");

        else if (n==2)

            printf ("MONDAY");

        else if (n==3)

            printf ("TUESDAY");

        else if (n==4)

            printf ("WEDNESDAY");

        else if (n==5)

            printf ("THURSDAY");

        else if (n==6)

            printf ("FRIDAY");

        else

            printf ("SATURDAY");

    }

    else

        printf ("Invalid Entry");

    return 0;

}

Here, the program first checks the condition whether entered number n is less than or equal to 7 or not. If it is less than 7 then the number is checked against the if and else if conditions and the statement associated with the matching condition is executed. If the entered number is more than 7 then “Invalid Entry” is displayed.

This program can also be written by using switch statement:

#include<stdio.h>

int main()

{

    int n;

    printf("Enter a number from 1 to 7 \n");

    scanf("%d", &n);

    switch (n)

    {

        case 1:  printf ("SUNDAY");

        break;

        case 2:  printf("MONDAY");

        break;

        case 3: printf ("TUESDAY");

        break;

        case 4: printf ("WEDNESDAY");

        break;

        case 5: printf ("THURSDAY");

        break;

        case 6: printf ("FRIDAY");

        break;

        case 7: printf ("SATURDAY");

        break;

        default: printf ("Invalid Entry");

    }

    return 0;

}


Here, the value in variable n is checked against a list of case labels and the statement associated with the matching case label is executed.

Output:

Enter a number from 1 to 7

2

MONDAY

 

Enter a number from 1 to 7

9

Invalid Entry