Computer Applications
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).
Example:
145 is a special number, because 1! + 4! + 5! = 1 + 24 + 120 = 145.
(Where ! stands for factorial of the number and the factorial value of a number is the product of all integers from 1 to that number, example 5! = 1 * 2 * 3 * 4 * 5 = 120)
Java
Java Iterative Stmts
ICSE 2011
95 Likes
Answer
import java.util.Scanner;
public class KboatSpecialNum
{
public static int fact(int y) {
int f = 1;
for (int i = 1; i <= y; i++) {
f *= i;
}
return f;
}
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;
while (t != 0) {
int d = t % 10;
sum += fact(d);
t /= 10;
}
if (sum == num)
System.out.println(num + " is a special number");
else
System.out.println(num + " is not a special number");
}
}Output

Answered By
33 Likes
Related Questions
Define a class to accept a number and check whether it is a SUPERSPY number or not. A number is called SUPERSPY if the sum of the digits equals the number of the digits.
Example1:
Input: 1021 output: SUPERSPY number [SUM OF THE DIGITS = 1+0+2+1 = 4, NUMBER OF DIGITS = 4 ]Example2:
Input: 125 output: Not an SUPERSPY number [1+2+5 is not equal to 3]Which of the following are entry controlled loops?
(a) for
(b) while
(c) do..while
(d) switch
- only a
- a and b
- a and c
- c and d
How many times will the following loop execute? Write the output of the code:
int x=10; while (true){ System.out.println(x++ * 2); if(x%3==0) break; }