write program in HTML to print 7 days in a week in table
Answers
Explanation:
/*
* C program to print day of week name
*/
#include <stdio.h>
int main() {
int day;
/*
* Take the Day number as input form user
*/
printf("Enter Day Number (1 = Monday ..... 7 = Sunday)\n");
scanf("%d", &day);
/* Input Validation */
if(day < 1 || day > 7){
printf("Invalid Input !!!!\n");
return 0;
}
if(day == 1) {
printf("Monday\n");
} else if(day == 2) {
printf("Tuesday\n");
} else if (day == 3) {
printf("Wednesday\n");
} else if(day == 4) {
printf("Thursday\n");
} else if(day == 5) {
printf("Friday\n");
} else if(day == 6) {
printf("Saturday\n");
} else {
printf("Sunday\n");
}
return 0;
}
/**
* C program to print day name of week
*/
#include <stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number (1-7): ");
scanf("%d", &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;
}
The above approach is easiest to code and understand. However, use of if...else is not recommended when checking condition with fixed constants.
You must prefer switch...case statement when checking conditions with fixed values.
Learn - Program to print day name of week using switch case
Another approach to solve the program is by defining day name string constants in array. Using array you can easily cut length of the program. Below program illustrate how to print day of week using array.