My code for gregorian calendar practise problem is giving me wrong answer after submission. Initially I took only 1 condition ( modulus of 4 ) for my leap year part, but I rectified my mistake and compiled it again. Even though I am getting correct output for all the inputs, it is giving me wrong answer.
The link of the problem is https://www.codechef.com/problems/FLOW015
Here is my code:
#include<stdio.h>
#include<math.h>
int main()
{
// MAIN FORMULA - (year code + month code + century code + Date number - Leap year code)%7
// Here as Date is 1 & Month is January hence DATE NUMBER is 1 and MONTH CODE is 0
// If leap year SUBSTRACT 1 ELSE DON'T
// Year code formula - (YY +(YY/4))%7 where in 1898 YY is 98
// Century code - 1800s - 2; 1900s - 0; 2000s - 6; 2100s - 4; 2200s - 2; 2300s - 0;
int number_of_years, year_code, month_code, yy, century_code, date_number, leap_year, year[1000], day, i, j;
month_code = 0;
date_number = 1;
scanf("%d",&number_of_years);
for(i = 0; i<number_of_years; i++)
{
scanf("\n%d",&year[i]);
}
for(j = 0; j<number_of_years; j++)
{ yy = 0; century_code = 0; year_code = 0; leap_year = 0; day = 0;
{
if(year[j]>=1800 && year[j]<=1899)
{century_code = 2; yy = year[j] - 1800; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=1900 && year[j]<=1999)
{century_code = 0; yy = year[j] - 1900; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2000 && year[j]<=2099)
{century_code = 6; yy = year[j] - 2000; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2100 && year[j]<=2199)
{century_code = 4; yy = year[j] - 2100; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2200 && year[j]<=2299)
{century_code = 2; yy = year[j] - 2200; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2300 && year[j]<=2399)
{century_code = 0; yy = year[j] - 2300; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2400 && year[j]<=2499)
{century_code = 6; yy = year[j] - 2400; year_code = ((yy+(yy/4))%7);}
else if(year[j]>=2500 && year[j]<=2599)
{century_code = 4; yy = year[j] - 2500; year_code = ((yy+(yy/4))%7);}
}
if((year[j]%4 == 0) && (year[j]%100 != 0))
{ leap_year = 1;}
else if(year[j]%400 == 0)
{ leap_year = 1;}
day = ((year_code + month_code + century_code + date_number - leap_year)%7);
switch(day)
{ case 0: printf("\nsunday") ;
break ;
case 1: printf("\nmonday") ;
break ;
case 2: printf("\ntuesday") ;
break ;
case 3: printf("\nwednesday") ;
break ;
case 4: printf("\nthursday") ;
break ;
case 5: printf("\nfriday") ;
break ;
case 6: printf("\nsaturday") ;
break ;
}
}
return 0;
}