Pages

Wednesday, 5 October 2016

Chapter 10 Exercise 21, Introduction to Java Programming, Tenth Edition Y. Daniel LiangY.

10.21 (Divisible by 5 or 6) Find the first ten numbers greater than Long.MAX_VALUE that are divisible by 5 or 6.


import java.math.BigDecimal;

public class Exercise_21 {

    public static void main(String[] args) {

        int count = 0;
        BigDecimal num = new BigDecimal(Long.MAX_VALUE).add(BigDecimal.ONE);
        while (count < 10) {

            if (num.remainder(new BigDecimal(5)).equals(BigDecimal.ZERO) ||
                num.remainder(new BigDecimal(6)).equals(BigDecimal.ZERO)) {
                count++;
                System.out.println(count+ ": " +num);
            }
            num = num.add(BigDecimal.ONE);
        }
    }
}

No comments:

Post a Comment