TheBulgarianWolf

Strong number

Apr 10th, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.98 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class SoftUni {
  4.     public static int factorial(int n){
  5.         if(n == 0){
  6.             return 1;
  7.         }
  8.         else{
  9.             return n*factorial(n-1);
  10.         }
  11.     }
  12.    
  13.    
  14.     public static void main(String[] args) {
  15.         Scanner sc = new Scanner(System.in);
  16.         System.out.print("Enter your number and I will check if it is a string number: ");
  17.         //A number is strong if the sum of the Factorial of each digit is equal to the number
  18.         int number = Integer.parseInt(sc.nextLine());
  19.         int check = number;
  20.         int sum = 0;
  21.         while(number > 0){
  22.             int current = number%10;
  23.             sum += factorial(current);
  24.             number /= 10;
  25.            
  26.         }
  27.        
  28.         if(check == sum){
  29.             System.out.println( sum + " is a strong number!");
  30.         }
  31.         else{
  32.             System.out.println(check + " is not a strong number!");
  33.         }
  34.        
  35.     }
  36.  
  37. }
Add Comment
Please, Sign In to add comment