Q.1) Write a program to print day of week name using switch case in C language.
#include < stdio.h>
int main()
{
int week;
/* Input week number from user */
printf("Enter week number(1-7): ");
scanf("%d", &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("Invalid input! Please enter week number between 1-7.");
}
getch();
}
Output
Enter week number(1-7): 1
Monday
Program Explanation
Step 1 : Include header files (#include< stdio.h> and #include< conio.h>).
Step 2 : Start with main function with return type.
Step 3 : parenthesis to start and end the program { }.
Step 4 : declare variables with data type i.e, 'Week' is an integer type so we use "int" data type.
Step 5 : Use output function printf() to print the output on the screen.
Step 6 : Use input function scanf() to get input from the user.
Step 7 : here, we have print week of the day using switch case, enter a week no.(1 to 7) only. we have press 1. it was go to case 1. then the statement was right. then print "Monday". we can not enter between 1 to 7 then print "Invalid input! Please enter week number between 1-7."
Step 8 : using getch() function to hold the screen.