In java, to find all Strong numbers in a given range we can use loop concepts such as while loop or for loop. In this article, we will learn through examples, detailed logic and program explanation for better understanding.
What is Strong Number?
A “Strong number,” often referred to as a “factorial number” or a number that is “very strong,” is a positive integer where the sum of the factorials of its digits equals the number itself. Essentially, if you take each digit of the number, calculate the factorial of each digit, and then sum these factorials, the result should be the original number if it is a Strong number.
Here’s how to determine if a number is a Strong number:
- Separate the number into its individual digits.
- Compute the factorial of each digit.
- Sum all the factorial values.
- Compare this sum to the original number. If they are equal, the number is a Strong number.
For example, 145 is a Strong number because:
- The digits are 1, 4, and 5.
- The factorial of 1 is 1, the factorial of 4 is 24, and the factorial of 5 is 120.
- The sum of these factorials is 1 + 24 + 120 = 145, which is the original number.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
Problem Statement
We need to write a java program to find all Strong numbers between 1 to n or we can say in the given range. The program should take two integer values num and lastNum as input range and find out all the strong numbers in the given range. This action will be done in our program.
Let’s see an example,
Example
For input range of integer numbers between 1 to 500, Output should be 1,2 and 145.
Explanation:
Input Integer numbers: 1 and 500
Factorials of 1 : 1
sum of factorials : 1
Hence, the input starting number(1) is equal to the sum of factorials. Therefore, it will print 1 and similarly, repeat the same process till 500 and gives output 1,2 and 145 as the "Strong numbers".
Logic to find all strong numbers in a given range
- The program takes two integer numbers as num and lastNum as a range.
- It loops (while or for loop) through each number from the num (inclusive) to the lastNum (inclusive) of the provided range.
- For each number, it initializes sum and digit and copies the current number num into n.
- Uses a nested loop (while or for) to calculate the sum of factorials of each digit of n.
- Inside the nested loop, it calculates the sum of the factorials of its individual digits. The factorial of each digit is computed using a separate method (fact(int y)), which uses a for loop.
- After exiting the nested loop, it compares sum with the original number (sum==num).
- If the sum matches the original number, it identifies it as a strong number and prints the output and it proceeds to the next number in the range for further checks.
Java program to print all Strong numbers in a given range
Program 1: Using while loop
In the below program we are using while loop to print all Strong numbers in a given range.
import java.util.Scanner;
public class StrongNumberRange {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter first number");
int num = s.nextInt();
System.out.println("Enter another number");
int lastNum = s.nextInt();
while (num <= lastNum) {
int sum = 0, digit;
int n = num;
while (n != 0) {
digit = n % 10;
int x = fact(digit);
sum = sum + x;
n = n / 10;
}
if (sum == num)
System.out.println(num + " is Strong number");
num++;
s.close();
}
}
static int fact(int y) {
int f = 1;
for (int i = y; i > 0; i--) {
f = f * i;
}
return f;
}
}
Output:
Enter first number
1
Enter another number
600
1 is Strong number
2 is Strong number
145 is Strong number
Java program explanation to print all strong numbers in a given range
- The program imports
Scanner
for input and defines a classStrongNumberRange
. - Takes two integers (
num
andlastNum
) from the user. - Iterates from
num
tolastNum
using awhile
loop. - Calculates the factorial sum of digits for each number (
num
) within the loop. - Checks if the computed sum equals the original number (
num
), indicating it’s a strong number. - Prints each strong number found.
- Uses a factorial function (
fact
) to compute factorials. - Closes the
Scanner
object (s
) to free resources after processing.
Program 2: Using for loop
In the below program we are using for loop to print all Strong numbers in a given range.
import java.util.Scanner;
public class StrongNumberRange {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter first number");
int num = s.nextInt();
System.out.println("Enter second number");
int lastNum = s.nextInt();
for (;num <= lastNum; num++) {
int sum = 0, digit;
for (int n = num;n != 0;) {
digit = n % 10;
int x = fact(digit);
sum = sum + x;
n = n / 10;
}
if (sum == num)
System.out.println(num + " is Strong number");
s.close();
}
}
static int fact(int y) {
int f = 1;
for (int i = y; i > 0; i--) {
f = f * i;
}
return f;
}
}
Output:
Enter first number
1
Enter second number
50000
1 is Strong number
2 is Strong number
145 is Strong number
40585 is Strong number
Java program explanation to print all strong numbers in a given range
- The java program imports
Scanner
for user input and defines a classStrongNumberRange
. - Takes two integers (
num
andlastNum
) from the user to define a range. - Uses a
for
loop to iterate through each number fromnum
tolastNum
. - Computes the sum of factorials of each digit of
num
using a nestedfor
loop. - Checks if the computed sum equals the original number (
num
), indicating it’s a strong number. - Prints each strong number found during the iteration.
- Defines a
fact
method to calculate factorial (fact(int y)
). - Closes the
Scanner
object (s
) after completing the loop to release resources.
Conclusion
In this tutorial, we have learnt writing the Java program to print all the strong numbers in a given range. We have understood the logic and seen writing the program using for loop and while loop in Java.
You can also check our amazing tutorials by clicking on below links:
- Java Program to print all odd numbers in a given range
- Understanding the Operators in Java
- Java program check if a number is divisible by 11
- Count the digits of a given number in Java
Happy Coding!!