C Programming Question Solution

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

1 Jan 1900 was a Monday.
Thirty days has September,
April, June and November.
All the rest have thirty-one,
Saving February alone,
Which has twenty-eight, rain or shine.
And on leap years, twenty-nine.
A leap year occurs on any year evenly divisible by 4, but not on a century unless it is divisible by 400.

How many Sundays fell on the first of the month during the twentieth century (1 Jan 1901 to 31 Dec 2000)?
Leap years: 1900-1904-1908..

1.  31 - January
2.  28/29 - Februar
3.  31- Mars
4.  30 - April
5.  31 - Mai
6.  30 - Juni
7.  31 - Juli
8.  31 - August
9.  30 - September
10. 31 - Oktober
11. 30 - November
12. 31 - Desember
101011010101

Solution:


  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. void main()
  4. {
  5.         int year = 1901, month = 1, day = 2, sundays = 0, dayOfMonth=1, februaryLength= 28;
  6.         // Day = day of the week. 1 = Monday 2 = Tuesday .. 7 = Sunday
  7.         for (year = 1901; year <= 2000; year++) // Step through years
  8.         {
  9.                 for (month = 1; month <= 12; month++) // Step through months
  10.                 {
  11.                         if (day == 7) // Check if the first is a Sunday
  12.                         {
  13.                                 sundays++;
  14.                         }
  15.  
  16.                         // 31 DAY MONTH
  17.                         if ( (month == 1) || (month == 3) || (month == 5) || month == 7 || month == 8 || month == 10 || month == 12)
  18.                         {
  19.                                 for (dayOfMonth = 1; dayOfMonth <= 31; dayOfMonth++)
  20.                                 {
  21.                                         day++;
  22.                                         if (day == 8)
  23.                                                 day = 1;
  24.                                 }
  25.                         }
  26.                         else if (month == 4 || month == 6 || month == 9 || month == 11)
  27.                         {
  28.                                 for (dayOfMonth = 1; dayOfMonth <= 30; dayOfMonth++)
  29.                                 {
  30.                                         day++;
  31.                                         if (day == 8)
  32.                                                 day = 1;
  33.                                 }
  34.                         }
  35.  
  36.                         else if (month == 2) // Check if leap year
  37.                         {
  38.                                 if (year % 4 == 0) // Leap year
  39.                                 {
  40.                                         februaryLength = 29;
  41.                                 }
  42.                                 else
  43.                                         februaryLength = 28;
  44.                                 for (dayOfMonth = 1; dayOfMonth <= februaryLength; dayOfMonth++)
  45.                                 {
  46.                                         day++;
  47.                                         if (day == 8)
  48.                                                 day = 1;
  49.                                 }
  50.                         }
  51.                 }
  52.  
  53.         }
  54.         printf("\nNumber of sundays: %d", sundays);
  55.         system("PAUSE");
  56. }


Learn More :