Write a C program to print day name of week

Topics

  1. The program starts by including the standard input/output library stdio.h.
  2. The main() function is declared and begins.
  3. Three variables are declared: dayNum to store the user input, dayOfWeek as an array of strings to store the names of the days of the week.
  4. The user is prompted to enter a number between 1 and 7 using printf() and scanf() functions.
  5. An if-else statement chain is used to check the value of the input variable dayNum.
  6. If dayNum equals 1, the program prints “Sunday” using the printf() function.
  7. If dayNum equals 2, the program prints “Monday”.
  8. If dayNum equals 3, the program prints “Tuesday”.
  9. If dayNum equals 4, the program prints “Wednesday”.
  10. If dayNum equals 5, the program prints “Thursday”.
  11. If dayNum equals 6, the program prints “Friday”.
  12. If dayNum equals 7, the program prints “Saturday”.
  13. If dayNum is not between 1 and 7, the program prints an error message using the printf() function.
  14. The main() function ends and the program terminates.
#include <stdio.h>

int main() {
    int dayNum;
    printf("Enter a number between 1 and 7: ");
    scanf("%d", &dayNum);

    if (dayNum == 1) {
        printf("Sunday\n");
    }
    else if (dayNum == 2) {
        printf("Monday\n");
    }
    else if (dayNum == 3) {
        printf("Tuesday\n");
    }
    else if (dayNum == 4) {
        printf("Wednesday\n");
    }
    else if (dayNum == 5) {
        printf("Thursday\n");
    }
    else if (dayNum == 6) {
        printf("Friday\n");
    }
    else if (dayNum == 7) {
        printf("Saturday\n");
    }
    else {
        printf("Invalid input. Please enter a number between 1 and 7.\n");
    }

    return 0;
}

OUTPUT

Enter a number between 1 and 7: 3
Tuesday


Enter a number between 1 and 7: 8
Invalid input. Please enter a number between 1 and 7.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *