0
program to check whether a number is special or not?
3 Réponses
+ 3
http://code.sololearn.com/cmO9X8Je9ELC
import java.util.Scanner;
public class Program
{
public static int factorial(int n) {
int fact = 1;
for (int i = 1; i <= n; i++) {
fact *= i;
}
return fact;
}
public static boolean isSpecial(int n) {
int sum = 0;
int n2 = n;
while (n2 > 0) {
sum += factorial(n2%10);
n2 = n2 / 10;
}
return (n == sum);
}
public static void main(String[] args) {
System.out.println("Special numbers");
Scanner sc = new Scanner(System.in);
System.out.println("Please enter a number: ");
int n = sc.nextInt();
System.out.println(isSpecial(n));
}
}
+ 1
Define "special number".
0
a number is said to be a special number when the sum of factorial of its digits is equal to the number itself. for example :145 is a special number as 1! +4!+5!=145