fork(4) download
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. class Main
  5. {
  6. public static void main (String[] args) throws java.lang.Exception
  7. {
  8. int year=1901;
  9. boolean isLeapYear=false;
  10. int totalSundays=0;
  11. int currentDay=2;//Starts on a Monday
  12. while(year<=2000){
  13. isLeapYear=false;
  14. if((year%4)==0){
  15. if((year%100)==0 && (year%400)==0){
  16. isLeapYear=true;
  17. } else if((year%100)==0 && (year%400)!=0){
  18. isLeapYear=false;
  19. } else {
  20. isLeapYear=true;
  21. }
  22. }
  23. for(int month=1;month<=12;month++){
  24. if(currentDay==7){
  25. totalSundays++;
  26. }
  27. if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12){
  28. //January,March,May,July,August,October,December
  29. currentDay+=3;
  30. } else if(month==4 || month==6 || month==9 || month==11){
  31. //April,June,September,November
  32. currentDay+=2;
  33. } else if(month==2 && isLeapYear){
  34. //February has 29 days in a Leap Year
  35. currentDay+=1;
  36. }
  37.  
  38. if(currentDay>7){
  39. currentDay=currentDay-7;
  40. }
  41. }
  42. year++;
  43. }
  44. System.out.println("The total number of Sundays that fell in the first of the month is: "+totalSundays);
  45. }
  46. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
The total number of Sundays that fell in the first of the month is: 171