Five weekends: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{Task/realazthat |description=October of 2010 had five Fridays, five Saturdays, and Five Sundays. Write a program to show all months that have this same characteristic from the ...")
 
(Re-doing the task without forms)
Line 1: Line 1:
{{task}}October of 2010 had five Fridays, five Saturdays, and Five Sundays. Write a program to show all months that have this same characteristic from the year 1900 through 2100 (Gregorian calendar).
{{Task/realazthat

|description=October of 2010 had five Fridays, five Saturdays, and Five Sundays. Write a program to show all months that have this same characteristic from the year 1900 through 2100 (Gregorian calendar).
=={{header|Java}}==
}}
<lang java>import java.util.Calendar;
import java.util.GregorianCalendar;
public class FiveFSS {
//dreizig tage habt september...
private static int[] month31 = {Calendar.JANUARY, Calendar.MARCH, Calendar.MAY,
Calendar.JULY, Calendar.AUGUST, Calendar.OCTOBER, Calendar.DECEMBER};
public static void main(String[] args){
for(int year = 1900; year <= 2100; year++){
for(int month:month31){
Calendar date = new GregorianCalendar(year, month, 1);
if(date.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
//months are 0-indexed in Calendar
System.out.println((date.get(Calendar.MONTH) + 1) + "-" + year);
}
}
}
}
}</lang>
Output (middle results cut out):
<pre>3-1901
8-1902
5-1903
1-1904
7-1904
12-1905
3-1907
5-1908
1-1909
10-1909
7-1910
...
12-2090
8-2092
5-2093
1-2094
10-2094
7-2095
3-2097
8-2098
5-2099
1-2100
10-2100</pre>

Revision as of 22:23, 22 October 2010

Task
Five weekends
You are encouraged to solve this task according to the task description, using any language you may know.

October of 2010 had five Fridays, five Saturdays, and Five Sundays. Write a program to show all months that have this same characteristic from the year 1900 through 2100 (Gregorian calendar).

Java

<lang java>import java.util.Calendar; import java.util.GregorianCalendar;

public class FiveFSS {

     //dreizig tage habt september...
     private static int[] month31 = {Calendar.JANUARY, Calendar.MARCH, Calendar.MAY,
           Calendar.JULY, Calendar.AUGUST, Calendar.OCTOBER, Calendar.DECEMBER};
     public static void main(String[] args){
           for(int year = 1900; year <= 2100; year++){
                 for(int month:month31){
                       Calendar date = new GregorianCalendar(year, month, 1);
                       if(date.get(Calendar.DAY_OF_WEEK) == Calendar.FRIDAY){
                             //months are 0-indexed in Calendar
                             System.out.println((date.get(Calendar.MONTH) + 1) + "-" + year);
                       }
                 }
           }
     }

}</lang> Output (middle results cut out):

3-1901
8-1902
5-1903
1-1904
7-1904
12-1905
3-1907
5-1908
1-1909
10-1909
7-1910
...
12-2090
8-2092
5-2093
1-2094
10-2094
7-2095
3-2097
8-2098
5-2099
1-2100
10-2100