Independent Reading


Good Numbers.

A "good number" is a number in which the sum of the individual digits of the original number equals the sum of the individual digits of the prime factors whose product is the original number. Note that 1 is not a prime number, and therefore is never considered a prime factor of any number. This means that prime numbers are always good numbers. Write a program that will determine if a number in the range 2-32,767 is a good number.


Input. Any number from 2 to 32,767. Prompt the user to "Please enter a number:" from the keyboard. One execution of the problem should handle one number. Hint: No more than 15 prime factors are needed to make up the product of a number in this range.


Output. The output should be the number that was entered, followed by either the word "GOOD" (if it was a good number) or "NOT GOOD" if it was not a good number).


Example. The sum of the digits in the number 22 is 4 (2 + 2 = 4). The prime factors whose product is 22 are 2 and 11. The sum of those digits is also 4 (2 + 1 + 1). The number 22 is, therefore, a good number. The output should be:




  • Use System.out.print ("message"); for prompting the user
  • Use the following function (embedded in a complete program) to read a number from the keyboard. For this assignment, you may assume that the user will always enter a valid integer.




public class Sample


   public static void main(String[] args)  throws IOException



         int n;

         n = readInteger();

         System.out.println ("Your number is: "  + n);




   public static int readInteger() throws IOException

   /* Read an integer from the keyboard */


      String s;

      char c;

        int i = 0;

      char[] charArray = new char[10];


      System.out.println ("Enter a number:");

      while ((c = (char) != '\n')

         charArray[i++] = c;


        s = String.copyValueOf(charArray, 0, i-1);

      return Integer.parseInt(s);