Monday, September 12, 2016

JAVA HOW TO PROGRAM, NINTH EDITION, DEITEL: CHAPTER SIX SOLUTIONS



Question: Perfect Numbers 

An integer number is said to be a perfect number if its factors, including
1 (but not the number itself), sum to the number. For example, 6 is a perfect number, because 6 = 1 + 2 + 3. Write a method isPerfect that determines if parameter number is a perfect number. Use this method in an application that displays all the perfect numbers between 1 and 1000. Display the factors of each perfect number to confirm that the number is indeed perfect. Challenge the computing power of your computer by testing numbers much larger than 1000. Display the results. 


Solution: Perfect Numbers 

/**
 Author: Free Coder
 www.freecoder247.blogspot.com
 */

public class PerfectNumbers {

  public static void main(String[] args) {

    // loop and print perfect numbers between 1 and 1000 using our perfectNum() method
    for (int num = 1; num <= 1000; num++) {
      if (isPerfect(num)) {
        System.out.println("The number " + num + " is a perfect number!");
      }
    }
 
  } // end main()

 
 
  public static Boolean isPerfect(int num) {
 
    // declare a variable to hold the sum of num's factors
    int sum = 0;
 
    // loop over all the numbers up to 'num' to determine if they are factors
    // if they are then add them to 'sum'
    for (int factor = 1; factor < num; factor++) {
      if (num % factor == 0) {
        sum += factor;
      }
    }
 
    // if 'sum' is equal to the number passed in then it's a perfect number
    if (sum == num) {
      return true;
    } else {
      return false;
    }
 
  } // end public method isPerfect()

} // end PerfectNumbers class
 

No comments:

Post a Comment