In this tutorial you will learn about the Program to check Special Number Program in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check Special Number Program in Java
Prerequisites
Before starting with this tutorial we assume that you are best aware of the following Java programming topics:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- loops in java
Special Number in Java
Number said to special if the sum of factorial of its digits is equal to the Given number.
Example: 145.
-> factor of 1! + factor of 4! + factor of 5!
-> 1 + 24 + 120.
-> 145.
So the number is a special number.
Program to check Special Number Program in Java
In this program we would find the given number is special number or not , first will take the input from the user and then we calculate sum of factor of number gives number it self it is Special number. Let have a look at the code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
// java program to fing given number is a special or not .. import java.util.Scanner; class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in); // decalring variables... int count, number, sum=0, temp; // taking value from user System.out.println("Enter a number"); number = in.nextInt(); temp = number; // Find the factorial of its digits. while(temp != 0){ count = temp%10; sum += factorial(count); temp = temp/10; } // Checking the sum is equal to the original number or not if(sum == number) System.out.println("Special number"); else System.out.println("Not a Special Number"); } //function to calculate the factorial of the digits private static int factorial(int no){ int fact=1; for(int i=2; i<=no; i++){ fact = fact*i; } return fact; } } |
Output
Special Number.
Not a Special Number.
In the above program, we have first declared and initialized a set variables required in the program.
- number = it will hold entered number.
- temp = it will hold temp the value of number.
- sum = for adding values
- count= count will count size of a number
- i= For iteration.
After Declaring values first we will take a number form user
and pass number to temp variable where we break the number
and find factor of its each digit and calculate sum of all digit.
if sum of all digit is equal to the original number the given number is a Special Number
As shown in image if number equal to the sum of digit factor then it is Special if not not a Special number.