Programming Challenge. Solve the following problem by writing a program in Java
ID: 3760691 • Letter: P
Question
Programming Challenge.
Solve the following problem by writing a program in Java that will answer the following question:
You are given the following pieces of information about the 20th century:
176
Jan. 1st 1900 was a Monday
30 days has September, April, June and November. All the rest have 31
except February which has 28 and 29 on a leap year.
A leap year occurs on every year divisible by 4 expect on a century unless it
is divisible by 400.
Using your knowledge and the info above, how many Fridays during the 20th
century were divisible by 3 or 5? (1 Jan 1901 to 31 Dec 2000).
Explanation / Answer
import java.util.*;
import java.lang.*;
class Main
{
public static void main (String[] args) throws java.lang.Exception
{
int year=1901;
boolean isLeapYear=false;
int totalSundays=0;
int currentDay=2;
while(year<=2000)
{
isLeapYear=false;
if((year%4)==0)
{
if((year%100)==0 && (year%400)==0)
{
isLeapYear=true;
}
else if((year%100)==0 && (year%400)!=0)
{
isLeapYear=false;
}
else
{
isLeapYear=true;
}
}
for(int month=1;month<=12;month++)
{
if(currentDay==7)
{
totalSundays++;
}
if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
currentDay+=3;
}
else if(month==4 || month==6 || month==9 || month==11)
{
currentDay+=2;
}
else if(month==2 && isLeapYear)
{
currentDay+=1;
}
if(currentDay>7)
{
currentDay=currentDay-7;
}
}
year++;
}
System.out.println("Total num of Sundays that come in the first of the month is: "+totalSundays);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.