C Program To Display day from the date

C Program To Display day from the date

Tech Knowledge C Program To Display day from the date Tech Knowledge

  1.  //C Program To Display day from date By Tech Knowledge
  2. #include <stdio.h>
  3. #include <stdlib.h>

  4. int main()
  5. {
  6.  
  7.  int date,month,year,i,count=0,n=2000,z;
  8.  int mo[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  9.  char slash;
  10.  
  11.   printf("\n***C Program To Display day from date***\n");
  12.         printf("\nEnter the date in given format- Date/Month/Year");
  13.         printf("\n\nEnter Date:- ");
  14.         scanf("%d %c %d %c %d",&date,&slash,&month,&slash,&year);
  15.  
  16.     if((month>12 || month==0) || (date>31 || date==0) || (year>2099 || year<2000)|| slash!='/')
  17.       {
  18.             printf("Invalid date entered!\n\n");
  19.        exit(0);
  20.       }   
  21.      
  22.        else if((month==2 && date>29) || (month==4 && date>30) || (month==6 && date>30) || (month==9 && date>30) || (month==11 && date>30) )
  23.         
  24.       {
  25.             printf("Wrong date entered");
  26.        exit(0);
  27.           }
  28.          
  29.          else 
  30.        {
  31.          
  32.          while(n<year)
  33.          {
  34.           z=n;
  35.           
  36.          if(z%4==0)
  37.           {
  38.           count=count+366;
  39.            
  40.            }
  41.            else
  42.            {
  43.          count=count+365;
  44.          
  45.          }
  46.          n++;
  47.           } 
  48.         } 
  49.         
  50.          for(i=1;i<month;i++)
  51.          {
  52.              if(i==2 && year%4==0)
  53.              mo[2]=29;
  54.          count=count+mo[i];
  55.          mo[2]=28;
  56.          }
  57.         
  58.          count=count+date;
  59.          count%=7;
  60.          printf("\nDay Of Entered Date :- ");

  61.  switch(count)
  62.  {
  63.   case 0:printf("Friday");break;
  64.   case 1:printf("Saturday");break;
  65.   case 2:printf("Sunday");break;
  66.   case 3:printf("Monday");break;
  67.   case 4:printf("Tuesday");break;
  68.   case 5:printf("Wednesday");break;
  69.   case 6:printf("Thursday");break;
  70.   
  71.  }
  72. }


Output


***C Program To Display day from date***

Enter the date in given format- Date/Month/Year

Enter Date:- 25/05/2020

Day Of Entered Date :- Monday



How Work this program?


The #include<stdlib.h> is a header file in c language that tells the compiler to include commands of stdlib.h file.

stdlib.h files added to use exit(0); function of its library.

On next line int main(), int is a return datatype of function and main() is function . Every program has main() function compulsory.


Every function in c program written in curly braces  '{'   '}' i.e in curly braces body of function will be written.


 int date,month,year,i,count=0,n=2001,z; this is variables having int datatype.

int mo[13]={0,31,29,31,30,31,30,31,31,30,31,30,31}; This 'mo' variable contains total days of every month .This mo[13] declared as a array.

 if((month>12 || month==0) || (date>31 || date==0) || (year>2099 || year<2001)|| slash!='/')  This if statement check the date entered by user in given form or not if user enter date in wrong format it gives Invalid date entered.


exit(0); Is a function of stdlib.h file. It tells the compiler to exit the program with the status 0.


else if((month==2 && date>29) || (month==4 && date>30) || (month==6 && date>30) || (month==9 && date>30) || (month==11 && date>30) ) - This else if statement check the date entered by user is in the month and check if user enter 29 in february month and these year not a leap year them it gives the output invalid date entered , as same for month and year.

  1. while(n<year)
  2.          {
  3.          z=n;
  4.         
  5.          if(z%4==0)
  6.          {
  7.          count=count+366;
  8.         
  9.          }
  10.          else
  11.          {
  12.          count=count+365;
  13.         
  14.          }
  15.          n++;
  16.          }
Above while loop checks the condition entered year by the user is not less than or equal to 2001. If this condition true then next if statement check entered year is leap year or not by z%4==0; 
if the condition gets true then its adds 366 days otherwise it adds 365 days to count variable. 

  1. for(i=1;i<month;i++)
  2.          {
  3.          count=count+mo[i];
  4.         }

Above for loop count days present in month . This loop repeated till i variable less than month.


  1. count=count+date;
count=count+date; statement add days entered by user .And count total days .

  1. count%=7;
  2. printf("\nDay Of Entered Date :- ");
count%=7; this statement find the day. 

  1.  switch(count)
  2.  {
  3.   case 0:printf("Friday");break;
  4.   case 1:printf("Saturday");break;
  5.   case 2:printf("Sunday");break;
  6.   case 3:printf("Monday");break;
  7.   case 4:printf("Tuesday");break;
  8.   case 5:printf("Wednesday");break;
  9.   case 6:printf("Thursday");break;
  10.   
  11.  }
Above switch case print the day.
return 0; is a statement of the exit status of the program i.e program end.

Comments