Last active
October 9, 2024 11:16
-
-
Save chankruze/cdebab82a6c956cba5380cb8f25d9a38 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.Scanner; | |
import java.util.List; | |
public class StrongNumber { | |
public static int factorial(int n) { | |
if (n < 1) return 1; | |
return n * factorial(n - 1); | |
} | |
public static void main(String args[]) { | |
Scanner sc = new Scanner(System.in); | |
List<Integer> strongNumbers = new ArrayList<>(); | |
int size = sc.nextInt(); | |
int i = 0; | |
// loop until positive numbers of queue size are received | |
while (i < size) { | |
System.out.println("Enter number " + i + ": "); | |
int num = sc.nextInt(); | |
if (num >= 0) { | |
i++; | |
int n = num; | |
int sum = 0; | |
// break the number to extract each digit | |
while (n > 0) { | |
int digit = n % 10; | |
// calculate factorial | |
int fact = factorial(digit); | |
// add to sum | |
sum += fact; | |
// update the number | |
n /= 10; | |
} | |
if (sum == num) { | |
strongNumbers.add(num); | |
} | |
} else { | |
System.out.println("Enter positive number only."); | |
} | |
} | |
System.out.print("Strong numbers are: "); | |
for (int strongNumber: strongNumbers) { | |
System.out.print(strongNumber + " "); | |
} | |
} | |
} |
Author
chankruze
commented
Oct 5, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment