You are currently viewing C Program To Print Week Day For A Given Input Week Using If-else

C Program To Print Week Day For A Given Input Week Using If-else

In this tutorial, you will learn how to write a program to print weekdays for a given input week using if-else statements. Let’s understand how this C program prints the weekday for a given input week using if-else statements.

Problem Statement

We have to write a C program that will print the weekday for a given input week number. Our program should take the input digits between 1 to 7 and print the weekday name.

Examples

Enter the week number (1-7): 1
Output: The weekday for week 1 is Monday.

Enter the week number (1-7): 4
Output: The weekday for week 4 is Thursday.

Enter the week number (1-7): 7
Output: The weekday for week 7 is Sunday.

Algorithm To Print Week Day For A Given Input Week

  1. Start the program.
  2. Declare a variable week of type int to store the input week number.
  3. Prompt the user to enter the week number by printing the message “Enter the week number (1-7): “.
  4. Read the input week number from the user and store it in the week variable using scanf().
  5. Use an if-else statement to determine the corresponding weekday based on the input week number:
    • If week is 1, print the message “The weekday for week 1 is Monday.”
    • If week is 2, print the message “The weekday for week 2 is Tuesday.”
    • If week is 3, print the message “The weekday for week 3 is Wednesday.”
    • If week is 4, print the message “The weekday for week 4 is Thursday.”
    • If week is 5, print the message “The weekday for week 5 is Friday.”
    • If week is 6, print the message “The weekday for week 6 is Saturday.”
    • If week is 7, print the message “The weekday for week 7 is Sunday.”
    • If week is not between 1 and 7, print an error message “Invalid week number. Please enter a number between 1 and 7.”
  6. End the program

Program to print week day for a given input week in C

#include <stdio.h>
int main() {
    int week;
    printf("Enter the week number (1-7): ");
    scanf("%d", &week);
    if (week == 1) {
        printf("The weekday for week 1 is Monday.\n");
    }
    else if (week == 2) {
        printf("The weekday for week 2 is Tuesday.\n");
    }
    else if (week == 3) {
        printf("The weekday for week 3 is Wednesday.\n");
    }
    else if (week == 4) {
        printf("The weekday for week 4 is Thursday.\n");
    }
    else if (week == 5) {
        printf("The weekday for week 5 is Friday.\n");
    }
    else if (week == 6) {
        printf("The weekday for week 6 is Saturday.\n");
    }
    else if (week == 7) {
        printf("The weekday for week 7 is Sunday.\n");
    }
    else {
        printf("Invalid week number. Please enter a number between 1 and 7.\n");
    }

    return 0;
}

Output

Enter the week number (1-7): 4
The weekday for week 4 is Thursday.

Program Explanation

  • The program starts by including the necessary header file stdio.h.
  • The main() function is defined.
  • Inside the main() function, a variable week of type int is declared to store the input week number.
  • The program prompts the user to enter the week number by printing the message “Enter the week number (1-7): “.
  • The scanf() function is used to read an integer from the user, and the input is stored in the week variable using the %d format specifier.
  • An if-else statement is used to determine the corresponding weekday based on the input week number:
    • The conditions week == 1, week == 2, …, week == 7 are checked to determine the week number.
    • If the week number matches one of the conditions, it prints the corresponding weekday message using printf().
    • If the week number is not between 1 and 7, it prints an error message “Invalid week number. Please enter a number between 1 and 7.”
  • The return 0 statement ends the program.

Conclusion

This C program allows the user to enter a week number and prints the corresponding weekday using if-else statements. It covers all possible cases from 1 to 7 and provides the appropriate output based on the input week number.