Strong number program in Java
In this post, you will learn how to write a strong number program using the Java programming language.
Factorial, in mathematics, the product of all positive integers less than or equal to a given positive integer and denoted by that integer and an exclamation point. The factorial of a number n is denoted by n!. This is the product of all positive numbers less than or equal to n. It is calculated as -
n! = n X (n-1) X (n-3) X ……… X 3 X 2 X 1
The factorial operation generally comes across many mathematical operations, like- combinatorics, algebra, analysis.
A strong number is a number in which the sum of the factorial of the digits is equal to the number itself. Like- 1, 2, 145, 40585, and so on. Example-
Input Number - 145
Sum of Factorial of the digits -
1! + 4! + 5!
1 + 24 + 120
145 // Strong Number
As you can see above, the input number is equal to the sum of the factorial of the digits. So, the number 145 is the factorial number. Again, let's check for the other number.
Input Number - 162
Sum of Factorial of the digits -
1! + 6! + 2!
1 + 720 + 2
723 // Not a Strong Number
Java Strong Number Program Source Code
Here is the program to check whether the input number is a strong number or not in Java.
In the given example, we have created an instance of the scanner class and declared variables to store the number, the sum of factorial of digits and a temp variable. Then, we ask the user to provide the number. The while loop calculates the sum of the factorial of digits.
Inside the while loop, the factorial of the last digit is calculated and added to the sum. In each iteration, the 'fact' variable is initialised to 1, as for every digit we have its own factorial, and the variable temp is divided by 10, so that we can get the last digit and then the second last digit, and so on. At last, we have checked if the computed sum is equal to the original number. If so, then the given number is a strong number, otherwise not.
import java.util.Scanner;
public class SpecialNumberProgram
{
public static void main(String args[])
{
int temp, input_num, last_digit, sum_of_fact = 0;
Scanner sc = new Scanner(System.in);
System.out.print("Please enter a number: ");
//reads an integer input from the user
input_num = sc.nextInt();
temp = input_num;
while (temp > 0)
{
//finds the last digit
last_digit = temp % 10;
//variable to store the factorial
int fact=1;
//calculates factoria
for(int i=1; i<=last_digit; i++)
{
fact=fact*i;
}
//calculates the sum of all factorials
sum_of_fact = sum_of_fact + fact;
temp = temp / 10;
}
//compares the sum with the given number
if(input_num ==sum_of_fact)
{
System.out.println(input_num+ " is a strong number.");
}
else
{
System.out.println(input_num+ " is not a strong number.");
}
}
}
Output of the above code:
Please enter a number: 145
145 is a special number.
Related Articles
Sort array in ascending order JavaAutomorphic number in Java
Pascal triangle program in Java
Factorial using recursion in java
Java random number between 1 and 10
Palindrome program in Java
Floyd triangle in Java
Pyramid pattern programs in Java
Star pattern programs in Java
Number pattern programs in Java
Java program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java