C Program To Count Days From 1/1/2001


C Program To Count Days From 1/1/2001

Tech Knowledge C Program To Count Days From 1/1/2001

  1. #include <stdio.h>
  2. #include <stdlib.h>

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


Output

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

Enter Date:- 22/04/2001
Total Days From 01/01/2001 To Date Entered By You =113


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;
  2.          printf("Total Days From 01/01/2001 To Date Entered By You =%d",count);   
And lastly count=count+date; statement add days entered by user .And prints total days on the screen.

return 0; is a statement of the exit status of the program i.e program end.


Comments