How To Check Whether a Number is a Special Number or Not in Java?


A number is said to be a Special number, if the sum of factorials of the input number’s every digit is equal to the same input number.

For more clarification, we have to find all the factors of every digit of the given number. After that we have to calculate the sum of those factorials. Then we have to compare both the sum value and input number, if they are the same then the given number is a special number otherwise it is not a special number.

In this article, we will see how to check if a number is a special number by using Java programming language.

To show you some instances

Instance-1

Input number is 145.

Let’s check it by using the logic of the special number.

The factorial of 1, 4 and 5 is 1, 24 and 120.
The sum of these factorials = 1 + 24 + 120 = 145

As we notice here both the sum of the factorials and input value are same.

Hence, 145 is a special number.

Instance-2

Input number is 534.

Let’s check it by using the logic of the special number.

The factorial of 5, 3 and 4 is 120, 6 and 24.
The sum of these factorials = 120 + 6 + 24 = 150

As we noticed here both the sum of the factorials and input value are not same.

Hence, 534 is not a special number.

Algorithm

Algorithm-1

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − Extract one by one digit by using modulo operator (%) and simultaneously find the factorial of each digit by using a while loop and keep track of the sum of factorials.

  • Step 3 − Finally compare the sum value with the input number.

  • Step 4 − If both the sum value and the input value are equal to each other then we can print the result as the given number is a special number otherwise the number is not a special number.

Algorithm-2

  • Step 1 − Get an integer number either by initialization or by user input.

  • Step 2 − Then take an array and keep the factorials of respective index positions. (At index−0 and index−1 keep value 1), index-2 will hold factorial of 2, index−3 will hold factorial of 3… and last index-9 will hold factorial of 9.

  • Step 3 − Extract one by one digit by using modulo operator (%) and based on the digit find its factorial from the array and keep track of the sum of factorials.

  • Step 4 − Finally compare the sum value with the input number.

  • Step 5 − If both the sum value and the input value are equal to each other then we can print the result as the given number is a special number otherwise the number is not a special number.

Multiple Approaches

We have provided the solution in 2 different approaches.

  • By Using Static Input Value

  • By Using User Defined Method and Array

Let’s see the program along with its output one by one.

Approach-1: By Using Static Input Value

In this approach, take a number in the program as static input and then by using the Algorithm−1, we can check whether the number is a special number or not.

Example

import java.util.*; public class Main { //main method public static void main(String[] args) { //declare an int variable and initialize a number as value int inputNumber = 145; //declare a variable for iteration int i; //declare variables for factorial value and the extracted digits int factorial,digit; //declare a variable to store the sum value int sum = 0; //transfer the input value to a temporary variable int temp = inputNumber; //start looping for calculating the result while(temp != 0) { i = 1; factorial = 1; //extracting the digit digit = temp % 10; //get the factorial of the digit while(i <= digit) { factorial = factorial * i; i++; } //store the sum value sum = sum + factorial; //removing the digit one by one temp = temp / 10; } //check condition if(sum == inputNumber) //if sum value is equal to input number System.out.println(inputNumber + " is a special number\n"); else //if sum value is not equal to input number System.out.println(inputNumber + " is not a special number\n"); } }

Output

145 is a special number

Approach-2: By Using User Defined Method and Array

In this approach, take a static number as input and pass this number as a parameter in a user defined method then inside the method by using the Algorithm−2 we can check whether the number is a special number or not.

Example

public class Main { //main method public static void main (String[] args) { //declare an int variable and initialize it with a number int inp = 2; //in if condition call the user defined function //by passing the input value to the method as parameter if(checkSpecial(inp)) { //if true then it is a special number System.out.println(inp + " is a special number."); } else { //if false then it is not a special number System.out.println(inp + " is not a special number."); } } //user defined method to check special number static boolean checkSpecial(int inputNumber) { //declare an array to store all the factorial value from 0 to 9 int factorial[] = new int[10]; //store 1 in 0th and 1st index of factorial //this is just to store each digits factorials at its respective index position //like the 1st index will hold factorial of 1, 2nd index for factorial of 2, 3rd index for factorial of 3... factorial[0] = factorial[1] = 1; //initiating the loop to find the factorials for (int i = 2; i<10; ++i) factorial[i] = factorial[i-1] * i; //declare an int variable 'sum' to store the sum value int sum = 0; //declare a temporary variable to store the input number int temp = inputNumber; //initiate the iteration for finding the sum of the factorials of the digits while (temp>0) { //get the factorial of the digit from the array sum += factorial[temp%10]; //removing the digit after calculation temp /= 10; } //if the sum value is equal to input number return true return (sum == inputNumber); } }

Output

2 is a special number.

In this article, we explored how to check a number whether it is a special number or not in Java by using different approaches.

Updated on: 17-Nov-2022

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements