Special number program in Java
In this post, you will learn how to find the special number program using the Java programming language. The special number program is frequently asked in Java coding tests and interviews.
In mathematics, Factorial is the product of all positive integers less than or equal to a given positive integer, and is 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, and analysis.
A number (N) is known as a special number when the sum of the factorial of digits is equal to the number itself. The number (N) is called a special number or strong number. 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, 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
Special number program in Java
In the given program, we ask the user to provide an integer input. Then, we use the for loop to find the factorial of a digit and add all the factorials. Next, we check if the computed sum is equal to the original number. If so, then the entered number is special, otherwise not.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int digit, num, sum=0, temp;
System.out.println("Please enter a number");
num = in.nextInt();
temp = num;
while(temp != 0){
digit = temp%10;
sum += factorial(digit);
temp = temp/10;
}
if(sum == num)
System.out.println(num+" is Special Number.");
else
System.out.println(num+" is not a Special Number.");
}
private static int factorial(int number){
int fact=1;
for(int i=2; i<=number; i++){
fact = fact*i;
}
return fact;
}
}
Output of the above code:
Please enter a number
145
145 is Special Number.
Please enter a number
110
110 is not 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