Advertisement
Anthei

ExtraCreditAssignment1

Oct 27th, 2014
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.29 KB | None | 0 0
  1. /* Eva Goins
  2.  * Extra Credit Assignment #1
  3.  * Description: An application that prints whole numbers
  4.  *      less than 6 digits (0-99999) where the sum of
  5.  *      the factorial of each digit is the number itself.
  6.  *      For example, IF 123 = 1! + 2! + 3!, the program
  7.  *      will print the number 123 (though this is not
  8.  *      one of the four). There are 4 such numbers. A
  9.  *      method uses an integer argument to convert it
  10.  *      into a string, in order to get each individual
  11.  *      character in order to calculate the factorial
  12.  *      sum of the number. If the number itself matches
  13.  *      the factorial sum of each of the digits in that
  14.  *      number, then the boolean flag returns true.
  15.  *      Otherwise, it will return false.
  16.  */
  17.  
  18.  public class ExtraCreditProgram1
  19.  {
  20.     public static void main(String[] args)
  21.     {
  22.         int num = 0;
  23.         int index;
  24.        
  25.         for (index = 0; index <= 99999; index++)
  26.         {
  27.             num = index;
  28.            
  29.             if (factorialCheck(num) == true)
  30.             {
  31.                 System.out.println(num);
  32.             }
  33.         }
  34.     }
  35.    
  36.     public static boolean factorialCheck(int number)
  37.     {
  38.         String strNum = number + "";
  39.         char charNum = ' ';
  40.         int inCount;
  41.         int strLength = strNum.length();
  42.         int factSum = 0;
  43.         boolean flag = false;
  44.        
  45.         for (inCount = 0; inCount < strLength; inCount++)
  46.         {
  47.             charNum = strNum.charAt(inCount);
  48.            
  49.             switch (charNum)
  50.             {
  51.                 case '0':
  52.                 case '1':
  53.                     factSum = factSum + 1;
  54.                     break;
  55.                 case '2':
  56.                     factSum = factSum + (1 * 2);
  57.                     break;
  58.                 case '3':
  59.                     factSum = factSum + (1 * 2 * 3);
  60.                     break;
  61.                 case '4':
  62.                     factSum = factSum + (1 * 2 * 3 * 4);
  63.                     break;
  64.                 case '5':
  65.                     factSum = factSum +
  66.                         (1 * 2 * 3 * 4 * 5);
  67.                     break;
  68.                 case '6':
  69.                     factSum = factSum +
  70.                         (1 * 2 * 3 * 4 * 5 * 6);
  71.                     break;
  72.                 case '7':
  73.                     factSum = factSum +
  74.                         (1 * 2 * 3 * 4 * 5 * 6 * 7);
  75.                     break;
  76.                 case '8':
  77.                     factSum = factSum +
  78.                         (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8);
  79.                     break;
  80.                 case '9':
  81.                     factSum = factSum +
  82.                         (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9);
  83.                     break;
  84.                 default:
  85.                     break;
  86.             }
  87.                
  88.             if (factSum == number)
  89.             {
  90.                 flag = true;
  91.             }
  92.             else
  93.             {
  94.                 flag = false;
  95.             }
  96.         }
  97.        
  98.         return flag;
  99.     }
  100.  }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement