Day of Week

Here is a simply C program that lets you enter a number from 1 -7 (valid options) or higher than 7 as an invalid entry. Using multiple if…else decisions the program will display a corresponding day of the week.

/*

Example 1.0 - Print the Day

Let us write a program that requests a number from 1 to
7 and prints the name of the day of the week. For    example, if the user enters 5, the program prints Thursday. Example 1.0 does the job using a series of if...else statements.

Compiler instructions:  gcc -o dow dow.c

*/


#include <stdio.h>
#include <stdlib.h> 

int main() {
    int n;
    void printDay(int);
    system("clear");
    printf("Enter a day from 1 to 7: ");
    scanf("%d", &n);
    printDay(n);
} // End main

void printDay(int d) {
    if (d == 1)      printf("Sunday\n");
    else if (d == 2) printf("Monday\n");
    else if (d == 3) printf("Tuesday\n");
    else if (d == 4) printf("Wednesday\n");
    else if (d == 5) printf("Thursday\n");
    else if (d == 6) printf("Friday\n");
    else if (d == 7) printf("Saturday\n");
    else printf("Invalid day\n");
    printf("\n");
} // End printDay
This entry was posted in C, Programming. Bookmark the permalink.

Leave a Reply

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