0

program to check whether a number is special or not?

10th Sep 2016, 12:46 PM
Simrah Mahmood
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)); } }
12th Sep 2016, 8:10 AM
Zen
Zen - avatar
+ 1
Define "special number".
10th Sep 2016, 12:48 PM
Zen
Zen - avatar
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
11th Sep 2016, 5:45 AM
Simrah Mahmood