Five weekends: Difference between revisions

Added extra credit requirement and added it to Java. Also scrolling the output.
(Added suggestions for how to do the task)
(Added extra credit requirement and added it to Java. Also scrolling the output.)
Line 4:
'''The task'''
# Write a program to show all months that have this same characteristic of five full weekends from the year 1900 through 2100 (Gregorian calendar).
# Show the ''number'' of months with this property (there should be 201).
# Show at least the first and last five dates, in order.
 
Line 11:
*Find all of the 31-day months that begin on Friday.
 
'''Extra credit'''
Show all of the years which do not have at least one five-weekend month (there should be 29).
=={{header|D}}==
<lang d>import std.stdio ;
Line 69 ⟶ 71:
import java.util.GregorianCalendar;
 
public class FiveFSS {
private static boolean[] years = new boolean[201];
//dreizig tage habt september...
private static int[] month31 = {Calendar.JANUARY, Calendar.MARCH, Calendar.MAY,
Line 77 ⟶ 80:
StringBuilder months = new StringBuilder();
int numMonths = 0;
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) {
years[year - 1900] = true;
numMonths++;
//months are 0-indexed in Calendar
months.append((date.get(Calendar.MONTH) + 1) + "-" + year + "\n");
}
}
}
System.out.println("There are " + numMonths + " with five weekends from 1900 through 2100:");
System.out.println(months);
System.out.println("Years with no five-weekend months:");
for (int year = 1900; year <= 2100; year++) {
if(!years[year - 1900]){
System.out.println(year);
}
}
}
}</lang>
Output (middle results cut out):
<pre style="height:30ex;overflow:scroll"> There are 201 with five weekends from 1900 through 2100:
3-1901
8-1902
Line 115 ⟶ 125:
5-2099
1-2100
10-2100</pre>
 
Years with no five-weekend months:
1900
1906
1917
1923
1928
1934
1945
1951
1956
1962
1973
1979
1984
1990
2001
2007
2012
2018
2029
2035
2040
2046
2057
2063
2068
2074
2085
2091
2096</pre>
 
=={{header|Python}}==
Anonymous user