Write a program to accept a number and check whether it is a special number or not .
Explanation :
A special number is a number such that the sum of the factorial of its digits is
equal to the number itself .
Sample Input : 145
Process : for 1 1! = 1
for 4! = 4 * 3 * 2 * 1
for 5! = 5 * 4 * 3 * 2 * 1
Sum of 1! + 4! + 5! = 145
Thus the sum equals the number itself and hence it is a special number .
import java.util.* ;
class specialNum
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in) ;
System.out.println("Enter a number") ;
int num = sc.nextInt() ;
int temp = num ;
int x ;
int factorial = 1;
int sum = 0 ;
while(temp>0)
{
x = temp%10 ;
for(int i=x;i>0;i--)
factorial *= i ;
sum += factorial ;
factorial = 1 ;
temp/=10 ;
}
if(sum==num)
System.out.println("Special Number");
else
System.out.println("Not a Special Number ") ;
}
}
Codely Prompt ----->java specialNum.java
Enter a number
145
Special Number