In this blog post, we learn how to write a C program to enter week number and print day name of week? We will write the C program to enter week number and print day of week. How to print day of week using if else in C programming. How to display the day name of week using if else in C programming. Write a C program to input week number(1-7) and print the corresponding day name of week name using if-else. How to print day of the week using switch case and an array of string. Logic to convert week number to a day of the week in C programming.
Example,
Input1 week number: 1 Output1: Monday Input2 week number: 5 Output2: Friday
C program to enter week number and print day name of week:
There are many ways to print days name by entering the days number. Let’s few of them,
C program to print day name of week using if-else:
The below program asks the user to enter the valid week number. After getting the value from the user program check the week number using the if-else condition and display the corresponding day. Here I assumed that Monday is first day of the week.
#include <stdio.h> int main() { unsigned int week; //Ask user to input week number printf("Enter week number (1-7): "); scanf("%u", &week); if(week == 1) { printf("Monday"); } else if(week == 2) { printf("Tuesday"); } else if(week == 3) { printf("Wednesday"); } else if(week == 4) { printf("Thursday"); } else if(week == 5) { printf("Friday"); } else if(week == 6) { printf("Saturday"); } else if(week == 7) { printf("Sunday"); } else { printf("Invalid Input! Please enter week number between 1-7."); } return 0; }
Output:
Enter week number (1-7): 1
Monday
Output 2:
Enter week number (1-7): 14
Invalid input! Please enter the week number between 1-7.
C program to print day name of week using switch-case:
The below program ask the user to enter the valid week number. After getting the value from the user program check the week number using the switch case and display the corresponding day. Like the above program also I assumed that Monday is the first day of the week.
#include <stdio.h> int main() { unsigned int week; //Ask user to input week number printf("Enter week number (1-7): "); scanf("%u", &week); switch (week) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; case 3: printf("Wednesday"); break; case 4: printf("Thursday"); break; case 5: printf("Friday"); break; case 6: printf("Saturday"); break; case 7: printf("Sunday"); break; default: printf("\n Please enter Valid Number between 1 to 7"); } return 0; }
Output:
Enter week number (1-7): 5
Friday
C program to print day name of week using an array:
The below program used a const string array to store the days on the corresponding array index. Now ask the user to enter the valid week number. After getting the value from the user get the days from the array using this value as an array index and display the day. Like the above program also I assumed that Monday is the first day of the week.
#include <stdio.h> int main() { unsigned int week; // Declare constant name of weeks const char * weekDays[] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" }; //Ask user to input week number printf("Enter week number (1-7): "); scanf("%u", &week); //prevent from go beyond array boundary if(week > 0 && week < 8) { // Print week name using array index printf("%s", weekDays[week-1]); } else { printf("Invalid input! Please enter week number between 1-7."); } return 0; }
Output 1:
Enter week number (1-7): 5
Friday
Output 2:
Enter week number (1-7): 29
Invalid input! Please enter week number between 1-7.
C program to print day name of using look-up table:
In the below code, I am mapping days of the week using a lookup table with the help of struct and array.
#include <stdio.h> /*Struct to hold mapping between day index and day name*/ struct Days { int index; const char* name; }; /* Lookup table for days and their name. */ const struct Days daysOfWeek[] = { {1, "Monday"}, {2, "Tuesday"}, {3, "Wednesday"}, {4, "Thursday"}, {5, "Friday"}, {6, "Saturday"}, {7, "Sunday"} }; /* Function to retrieve the day name based on the index. */ const char* getDayName(int index) { // Default value if index is out of range char *pName = "Invalid day"; //prevent from go beyond array boundary if(index > 0 && index < 8) { pName = daysOfWeek[index-1].name; } return pName; } int main() { unsigned int dayIndex; //Ask user to input week number printf("Enter days number (1-7): "); scanf("%u", &dayIndex); // Get the name of the day based on the index const char* dayName = getDayName(dayIndex); printf("%s\n",dayName); return 0; }
Recommended Post:
- C program to calculate the power of a number.
- Check if one integer is an integer power of another.
- C Program to check whether a given number is power of two.
- Pow function in C.
- sqrt function in C.
- C program to find all roots of a quadratic equation using switch case.
- C program to find the roots of a quadratic equation.
- How to find whether a given number is prime number in C?
- C Program to swap two nibbles in a byte
- C Program to find first and last digit of a given number
- C program to find even and odd numbers
- C Program to find the sum of natural numbers up to n terms
- C Program to find the sum of even natural numbers from 1 to n