19.How many Sundays?

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)?

import java.util.*;
public class Problem19 {
    public void Sundays() {
        int sun =0;
        for (int i=1901;i<=2000;i++) {
            for (int j=0;j<=11;j++) {
                Calendar cal = Calendar.getInstance();
                cal.set(i,j,1);
                if(cal.get(Calendar.DAY_OF_WEEK)==Calendar.SUNDAY) sun++;
            }
        }
            System.out.println(sun);
    }
    public static void main(String[] args) {
        Problem19 p = new Problem19();
        p.Sundays();
    }
}
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: