Menu

Sum of the Factorials of Digits of a Number -Java Program

Display sum of factorials of individual digits of a number

import java.util.*;
class Sum_factorial_digits{
public static void main(String args[]){

Scanner sc= new Scanner(System.in);
System.out.println("Enter a number to perform sum of factorial of digits");
int number=sc.nextInt();
int sum=0;
Hello h= new Hello();

if(number<0)                System.out.println("Invalid number");
if(number==0)              System.out.println(" The sum of factorial of given number is " + 1);

if(number>0)
{
while(number>0)
{              
int number1 = number%10;
sum=sum+h.fact(number1);
number= number/10;
}
System.out.println("The sum of factorial of given number is "+sum);
}

}}

class Hello
{
int fact(int n)
{
if(n==1 || n==0) return 1;
else return n*fact(n-1);

}
}