In Java, to check a given number is strong or not we can use loop concepts such as while loop or for loop. For given integer input value, find the sum of the factorials of individual digits. If sum of individual factorials of digits is equal to the original number we say that is a Strong number.
In this article, we will learn writing program to check given number is Strong or not through examples, detailed logic and program explanation for better understanding.
Required Knowledge
- Java programming basics
- Java Operators
- Loop Concepts
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.
Problem Statement
We need to write a Java program to check a given number is strong or not. The program should take an integer value num as input and identify whether it is a strong number or not and output the result. This action will be done in our program. Let’s see an example,
Example 1:
For an integer input number 145, Output should be “Strong number”.
Explanation:
Input integer number = 145
Calculating sum of factorials: 1+(1*2*3*4)+(1*2*3*4*5) = 1+24+120 = 145
Hence, input number(145) is equal to the sum of factorials of number 145. Therefore, 145 is a strong number.
Example 2:
For an integer input number 6, Output should be “Not a Strong number”.
Explanation:
Input integer number = 16
Calculating sum of factorials: 1+ (1*2*3*4*5*6) =1+720=721
Hence, input number(16) is not equal to the sum of factorials of number 16. Therefore, 16 is not a strong number.
Logic to check a given number is Strong number or not
- Take a number (num) from the user.
- Initialize the variable sum to 0 and declare another variable digit to store each digit of num during iteration.
- Assign num to n.
- A loop (while or for loop) iterates through each digit of num until n becomes 0.
- Inside the loop, digit=n%10, extracts the last digit of num.
- fact(digit) calls the fact method with digit as an argument to compute its factorial and assigns the result to variable x.
- fact method computes the factorial of the integer y.
- It initializes f to 1 and then iterates from y down to 1, multiplying each number to f and returns the computed factorial f.
- Add factorial of the current digit to the sum (sum=sum+x).
- Remove the last digit from num using n=n/10.
- After the loop completes, the using an if-else statement, check whether sum equals num.
- If true, it prints that num is a strong number, otherwise, it prints that num is not a strong number.
Java program to Check a given number is Strong or not
Program 1: Using while loop
In the below program we are using while loop to check a given number is strong or not in Java.
import java.util.Scanner;
public class StrongNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num = s.nextInt();
int sum=0, digit, 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");
else
System.out.println(num+" is not a 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 a number
145
145 is Strong number
Java program explanation to check a given number is strong or not
- This java program checks whether a given number is a strong number or not.
- Program takes user input using the Scanner class.
- The main method prompts the user to enter a number and reads it into the variable
num
. - It initializes variables
sum
anddigit
to 0 andn
tonum
for using n for iterations and stores updated value after each iteration. - Using a while loop, it extracts each digit of the number
num
. - For each digit, it calculates its factorial using the
fact()
method and adds it to thesum
. - After processing all digits, it checks if the sum of factorials is equal to the original number
num
. - If the sum matches
num
, it prints thatnum
is a strong number; otherwise, it prints that it’s not. - The
fact()
method calculates the factorial of a given number. - Finally, it closes the Scanner object to release resources.
Program 2: Using for loop
In the below program we are using for loop to check a given number is strong or not in Java.
import java.util.Scanner;
public class StrongNumber {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter a number");
int num = s.nextInt();
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");
else
System.out.println(num+" is not a 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 a number
156
156 is not a Strong number
Java program explanation to check a given number is strong or not
- The above java program imports the Scanner class for user input.
- The main method asks the user to enter a number and reads it into the variable
num
. - It initializes variables
sum
anddigit
to 0. - Enters a for loop where
n
is initialized tonum
, and the loop continues untiln
becomes 0. - Inside the loop, it extracts each digit of
num
using the modulus operator and stores it indigit
. - It calculates the factorial of the digit using the
fact()
method and adds it to thesum
. - After processing all digits, it checks if the sum of factorials is equal to the original number
num
. - If the sum matches
num
, it prints thatnum
is a strong number; otherwise, it prints that it’s not. - The
fact()
method calculates the factorial of a given number. - Finally, it closes the Scanner object to release resources.
Conclusion
In this article, we have learnt how to check if a number is strong number or not in Java. We have learnt the logic and moved on to writing the program using while loop or loop. Hope this article helped you in understanding the program.
For more information on related topics, you might find this articles helpful:
- Java program to find strong numbers in a given range
- Check division of students on the basis of marks in Java
- Java program to check if a person is valid to vote or not
- Palindrome number in Java
Happy Programming!!