ad

Sunday, February 23, 2025

C Program To Print Day of Week using If else case in switch case

c program     

https://webdesigningtheory.blogspot.com/

  C Program To Print Day of Week using  switch case

Example :-


#include<stdio.h>
#include<conio.h>
void main()
{
int num;
clrscr();
printf("enter a number between 1 to 7 :-");
num=getchar ()-'0';
if (num>=1&& num<=7)
{
switch(num)
{
case 1:
printf("monday \n");
break;
case 2:
printf("tuesday \n");
break;
case 3:
printf("wednesday \n");
break;
case 4:
printf("Thursday \n");
break;
case 5:
printf("Friday \n");
break;
case 6:
printf("Saturday \n");
break;
case 7:
printf("Sunday \n");
break;}
}
else{
printf("enter number between 1 to 7");
}
getch();

}



C Program To Print Day of Week using  switch case

Explain Program

Step :1

#include <stdio.h>

 input-output use printf() & scanf() function.

Step :2

int main()

 point of the C program. All execution .

Step :4

int num;

  • Declares an integer variable num.
  • It will store the number the user enters.

Step :5

printf("enter a number between 1 to 7 :-");

  • Displays message to the user to input a number between 1 and 7.

Step :6

num = getchar() - '0';

  • getchar() reads one character from the keyboard (like '1', '2', etc.).
  • Subtracting '0' converts the character to an integer.


Step :7

if (num >= 1 && num <= 7)

  • Checks whether the entered number is between 1 and 7.
  • If true, enter the switch block.
  • If false, go to the else block and print an error message.

Step :8

switch(num)

  • A multi-way branch statement.
  • Based on the value of num, it goes to the correct case.

Step :9

case 1: printf("monday \n"); break;

case 2: printf("tuesday \n"); break;

case 3: printf("wednesday \n"); break;

case 4: printf("Thursday \n"); break;

case 5: printf("Friday \n"); break;

case 6: printf("Saturday \n"); break;

case 7: printf("Sunday \n"); break;

Step :10

else

  • Runs if the user enters something not between 1 and 7.
  • Displays the message: "enter number between 1 to 7".

Output :-

Enter a number between 1 ro 7 :-  5
Friday 

Related post :-

javascript do while loop

Project navigation bar page

php number increasing reverse pyramid


No comments:

Post a Comment