- The program starts by including the standard input/output library
stdio.h
. - The
main()
function is declared and begins. - 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. - The user is prompted to enter a number between 1 and 7 using
printf()
and scanf()
functions. - An if-else statement chain is used to check the value of the input variable
dayNum
. - If
dayNum
equals 1, the program prints “Sunday” using the printf()
function. - If
dayNum
equals 2, the program prints “Monday”. - If
dayNum
equals 3, the program prints “Tuesday”. - If
dayNum
equals 4, the program prints “Wednesday”. - If
dayNum
equals 5, the program prints “Thursday”. - If
dayNum
equals 6, the program prints “Friday”. - If
dayNum
equals 7, the program prints “Saturday”. - If
dayNum
is not between 1 and 7, the program prints an error message using the printf()
function. - 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.