Java program to find strong number in an array
This tutorial will guide you to learn how to find strong number in an array in Java
Java is also a high programming level language and also easy to learn.
An array is a data type in java that consists of various data separated by a comma and enclosed in square bracket.
So a number is a strong number if the sum of the factorial of its digit is number itself.
Eg 145 here the digits are 1,4 and 5 so factorial of 1 is, 4! is 24 and 5! is 120 after adding 1+24+120=145
.
How to find strong number in an array in Java
public class strongNumber { public static void main(String[] args) { int temp, reminder, j,i, fact; int[] array={1,2,9,145,452}; for (j=0;j<array.length;j++) { int sum=0; fact = 1; i = 1; temp=array[j]; reminder = temp % 10; while (i <= reminder) { fact = fact * i; i++; } sum = sum + fact; temp = temp /10; if ( array[j] == sum ) { System.out.println("\n " + array[j] + " is a Strong Number"); } } } }
OUTPUT: 1 is a Strong Number 2 is a Strong Number 145 is a Strong Number
So for coding, we will first take the input array from the user we will extract digit by digit and pass the array after that we initialize a
for loop of array length and then we calculate factorial of each digit. Factorial is calculated we will add up and compare it with the
original number. So the number is equal to the sum of the factorial of its digit it is a strong number we will print it.
You may also read:
Leave a Reply