Sum of Factorial Digits
This program calculates sum of factorials of digits of a number.
Problem Statement
Write a Java program that calculates sum of factorials of digits of 145.
Source Code
| 1 | public class FactorialDigits { |
| 2 | public static void main(String[] args) { |
| 3 | int num = 145, temp = num, sum = 0; |
| 4 | while(temp > 0) { |
| 5 | int digit = temp % 10; |
| 6 | int fact = 1; |
| 7 | for(int i=1; i<=digit; i++) { |
| 8 | fact *= i; |
| 9 | } |
| 10 | sum += fact; |
| 11 | temp /= 10; |
| 12 | } |
| 13 | System.out.println("Sum of factorial digits = " + sum + " - Eduinq"); |
| 14 | } |
| 15 | } |
Program Output
Sum of factorial digits = 145 - Eduinq
Explanation
The program extracts digits, calculates factorial for each digit, and adds to sum. This demonstrates nested loops for factorial digit sums. Shows how loops can calculate sum of factorial digits.