ICSE Class 9 Computer Applications
Question 19 of 29
Iterative Constructs in Java — Question 22
Back to all questions 22
Question Question 18
Write a program to input a number and print whether the number is a special number or not. (A number is said to be a special number, if the sum of the factorial of the digits of the number is same as the original number).
import java.util.Scanner;
public class KboatSpecialNum
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();
int t = num;
int sum = 0, fact;
while (t != 0) {
int d = t % 10;
fact = 1;
for (int i = 1; i <= d; i++)
fact *= i;
sum += fact;
t /= 10;
}
if (sum == num)
System.out.println(num + " is a special number");
else
System.out.println(num + " is not a special number");
}
}Output
