Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Eva Goins
- * Extra Credit Assignment #1
- * Description: An application that prints whole numbers
- * less than 6 digits (0-99999) where the sum of
- * the factorial of each digit is the number itself.
- * For example, IF 123 = 1! + 2! + 3!, the program
- * will print the number 123 (though this is not
- * one of the four). There are 4 such numbers. A
- * method uses an integer argument to convert it
- * into a string, in order to get each individual
- * character in order to calculate the factorial
- * sum of the number. If the number itself matches
- * the factorial sum of each of the digits in that
- * number, then the boolean flag returns true.
- * Otherwise, it will return false.
- */
- public class ExtraCreditProgram1
- {
- public static void main(String[] args)
- {
- int num = 0;
- int index;
- for (index = 0; index <= 99999; index++)
- {
- num = index;
- if (factorialCheck(num) == true)
- {
- System.out.println(num);
- }
- }
- }
- public static boolean factorialCheck(int number)
- {
- String strNum = number + "";
- char charNum = ' ';
- int inCount;
- int strLength = strNum.length();
- int factSum = 0;
- boolean flag = false;
- for (inCount = 0; inCount < strLength; inCount++)
- {
- charNum = strNum.charAt(inCount);
- switch (charNum)
- {
- case '0':
- case '1':
- factSum = factSum + 1;
- break;
- case '2':
- factSum = factSum + (1 * 2);
- break;
- case '3':
- factSum = factSum + (1 * 2 * 3);
- break;
- case '4':
- factSum = factSum + (1 * 2 * 3 * 4);
- break;
- case '5':
- factSum = factSum +
- (1 * 2 * 3 * 4 * 5);
- break;
- case '6':
- factSum = factSum +
- (1 * 2 * 3 * 4 * 5 * 6);
- break;
- case '7':
- factSum = factSum +
- (1 * 2 * 3 * 4 * 5 * 6 * 7);
- break;
- case '8':
- factSum = factSum +
- (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8);
- break;
- case '9':
- factSum = factSum +
- (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9);
- break;
- default:
- break;
- }
- if (factSum == number)
- {
- flag = true;
- }
- else
- {
- flag = false;
- }
- }
- return flag;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement