48 : C Program to Display day of week using switch case

Check out the complete list of c-programs : C Program List    





User inputs a number between 1-7 and program outputs equivalent day on the console using switch case logic.

Logic :

1 => Sunday
2 => Monday
3 => Tuesday
4 => Wednusday
5 => Thursday
6 => Friday
7 => Saturday

Any other number 0, 8,9 etc : Invalid!


  switch(day)  {

     case 1:
        //Sunday
     break;
     
      case 2:
        //Monday   
     break;
     
      case 3:
        //Tuesday
     break;
     
      case 4:
        //Wednesday
     break; 
     
     case 5:
       //Thursday
     break;
     
      case 6:
        //Friday
     break;
     
      case 7:
        //Saturday
     break;
     
   default:
        //Invalid

}



Variables :

number : Datatype int : Holds value of year inputted by the user. %d is the Format specifier for int's

i : Datatype int : variable used in for loop

unsigned : Datatype long : To hold result. As factorial cannot be negative we set it as unsigned








/*
 * 1000+ C programs + tutorials
 *
 * 
 * 43_Display_day_of_week_switch_case_C_program.c
 *
 *
 *  Created on: Oct 20, 2014
 *  Author: Code2care.org
 */

#include 
//#include 



void main() {

int day;

  

//clrscr();

    printf("C Program Displays Day of Week for entered number");

    printf("\n\n Enter Day (1-7) : ");
    scanf("%i", &day);

    
  switch(day)  {

     case 1:
    printf("\n\n The 1st day of week is : Sunday");
     break;
     
      case 2:
    printf("\n\n The 2nd day of week is : Monday");
     break;
     
      case 3:
    printf("\n\n The 3rd day of week is : Tuesday");
     break;
     
      case 4:
    printf("\n\n The 4th day of week is : Wednesday");
     break; 
     
     case 5:
    printf("\n\n The 5th day of week is : Thursday");
     break;
     
      case 6:
    printf("\n\n The 6th day of week is : Friday");
     break;
     
      case 7:
    printf("\n\n The 7th day of week is : Saturday");
     break;
     
   default:
    printf("Invalid input!!! ..... ");

}
    
//getch();


}









The best way to learn C programming is to practice more and more of programs . Code2care C Programming tutorials provide 1000+ programs in C that you can study and become an expert in the language. Programs are divided into categories depending upon type and complexity.

BSc. IT, BSc. Computer Science, BSc. IT, MSc. IT, MSc. Computer Science, MCA, BTech IT & BTech Engineers may find these programs very useful.