Advertisements
Advertisements
Question
Write a program to enter a number and print the sum of factorial of the digits.
Code Writing
Advertisements
Solution
import java.util.*;
class clDigits
{
void main()
{
Scanner sc = new Scanner(System.in);
int N, d, fd, sum = 0;
System.out.print("Enter a number : ");
N = sc.nextInt();
int cn = N;
while(cn > 0)
{
d = cn % 10;
fd = 1;
for(int k = 1; k <= d; k++)
{
fd = fd * k;
}
sum = sum + fd;
cn = cn / 10;
}
System.out.println("Sum of factorial of digits of " + N + " : " + sum);
}
}
Output:
Enter a number : 254
Sum of factorial of digits of 254: 146
shaalaa.com
Is there an error in this question or solution?
