Here's the problem:
How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
And here's my code:
#include <stdio.h>
int main(void){
int months[12] = { 31 , 28 , 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 , 31 };
int year=1901;
int dayoftheweek=2; //put a 2 because the 1 jan 1901 is tuesday. 0-sunday 6-Saturday
int sunday=0;
int firstsundays=0;
while(year<=2000){
if(year%4==0) //checking if it's a leap year
months[1]=29;
for (int i = 0; i < 12; ++i) {
for (int d = 1; d <= months[i]; d++){
if(dayoftheweek==7) //reset the week
dayoftheweek=0;
if(dayoftheweek==sunday && d==1)
firstsundays++;
dayoftheweek++;
}
}
months[1]=28;
year++;
}
printf("There are %d Sundays that fell on the first of the month\n", firstsundays);
return 0;
}
What could I improve?