Project Euler Problem 19 Solution

Problem 19: Counting Sundays

You are given the following information, but you may prefer to do some research for yourself.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?

Solution 19: Counting Sundays

static int Problem019Solution() { //Set intial conditions int dayofWeek = 2; //1st of Jan 1901 = Tuesday. int firstMonthSundays = 0; //start count of Sundays that fall on the first of the month. for (int i = 1901; i <= 2000; i++) //Loop over each year { for (int j = 1; j <= 12; j++) //Loop over each month { Console.WriteLine("i = {0}, j = {1}", i, j); int monthDays = NumberOfDays(i, j); //Returns number of days, given month and year dayofWeek += monthDays % 28; if (dayofWeek % 7 == 0) { firstMonthSundays += 1; dayofWeek = 0; } } } { return firstMonthSundays; } } static int NumberOfDays(int Year, int month) { int monthDays = 0; if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { monthDays = 31; } if (month == 4 || month == 6 || month == 9 || month == 11) { monthDays = 30; } if (month == 2 && Year%4 == 0) { monthDays = 29; } if (month == 2) { monthDays = 28; } {return monthDays;} }

 My Comments

My Comments about the Solution are to follow here