Friday, 19 August 2016

Chapter 5 Exercise 27, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

**5.27 (Display leap years) Write a program that displays all the leap years, ten per line, from 101 to 2100, separated by exactly one space. Also display the number of leap years in this period.

public class ProgrammingEx5_27 {
 public static void main(String[] args) {
 
  int noOfLeapYear = 0;
 
  for (int year = 101; year <= 2100; year++) {
   boolean isLeapYear = (year % 4 == 0 && year % 100 != 0)
     || (year % 400 == 0);
 
   if (isLeapYear) {
    noOfLeapYear++;
    System.out.print(year + " ");
    if (noOfLeapYear % 10 == 0) {
     System.out.println();
    }
   }
  }
 
  System.out.println("\nThe number of leap year between 100 and 2100 is "
    + noOfLeapYear);
 
 }
}

No comments :

Post a Comment