Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Jaclyn Jardell
- * Extra Credit 1
- * An application that prints all whole numbers with
- * 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! your program would
- * print the number 123. There are 4 such numbers.
- */
- public class ExtraCredit1
- {
- public static void main (String [] args)
- {
- int num;
- int index;
- for (index = 0; index <= 99999; index++)
- {
- num = index;
- if (check(num) == true) System.out.println(num);
- }
- }
- public static boolean check (int number)
- {
- String strNum = num + "";
- char charNum = '';
- int count;
- int strLength = strNum.length();
- int sum = 0;
- boolean flag = false;
- for (count = 0; count < strLenngth; count++)
- {
- charNum = strNum.charAt(count);
- switch (num)
- {
- case '0':
- case '1':
- sum = sum + 1;
- break;
- case '2':
- sum = sum + (1 * 2);
- break;
- case '3':
- sum = sum + (1 * 2 * 3);
- break;
- case '4':
- sum = sum + (1 * 2 * 3 * 4);
- break;
- case '5':
- sum = sum + (1 * 2 * 3 * 4 * 5);
- break;
- case '6':
- sum = sum + (1 * 2 * 3 * 4 * 5 * 6);
- break;
- case '7':
- sum = sum + (1 * 2 * 3 * 4 * 5 * 6 * 7);
- break;
- case '8':
- sum = sum + (1 * 2 * 3 * 4 * 5 *6 * 7 * 8);
- break;
- case '9':
- sum = sum + (1 * 2 * 3 * 4 * 5 * 6 * 7 * 8 * 9);
- break;
- default:
- break;
- }
- }
- if (sum == num)
- {
- flag = true;
- }
- else
- {
- flag = false;
- }
- return flag;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement