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)
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
Related Questions
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; }Rewrite the following do while program segment using for:
x = 10; y = 20; do { x++; y++; } while (x<=20); System.out.println(x * y );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