*59*

# Special Number in Java

In this section, we will learn **what is a special number** and also create **Java programs** to check if the given number is a **special number** or not. The **special number program** frequently asked in Java coding tests and academics.

## Special Number

If the sum of the factorial of digits of a number (N) is equal to the number itself, the number (N) is called a **special** number.

Let’s understand it through an example.

### Special Number Example

Consider a number **145** and check it is a special number or not.

The digits of the number are: **1, 4, 5**

Factorial of digits:

!1 = **1**

!4 = 4*3*2*1 = **24**

!5 = 5*4*3*2*1 = **120**

Sum of factorial of digits = 1 + 24 + 120 = **145**

Compare the sum of the factorial of digits with the given number, i.e. **145 = 145**. We observe that both are equal.

Hence, the given number **145** is a **special** number.

Let’s check another number **1034**.

The digits of the number are: **1, 0, 3, 4**

Factorial of digits:

!1 = **1**

!0 = **1**

!3 = 3*2*1 = **6**

!4 = 4*3*2*1 =**24**

Sum of factorial of digits = 1 + 1 + 6 + 24 = **32**

Compare the sum of the factorial of digits with the given number, i.e. **32 ≠ 1034**. We observe that the sum of factorial of digits is not equal to the given number.

Hence, the given number **1034** is not a **special** number.

Some other special numbers are 2, 40585, etc.

## Steps to Find Special Number

- Read or initialize a number (N).
- Split the given number (N) into digits if the number has more than one digit.
- Find the factorial of all digits.
- Sum up the factorial and store it in a variable (s).
- Compare the sum with the given number (N).
- If the sum is equal to the number itself, the number (N) is a special number, else not.

Let’s implement the above logic in a Java program.

## Special Number Java Program

**SpecialNumberExample1.java**

**Output 1:**

Enter a number: 40585 40585 is a special number.

**Output 2:**

Enter a number: 176 176 is not a special number.

### Special Number Within a Range

**SpecialNumberExample2.java**

**Output:**

Enter the lower range: 1 Enter the upper range: 100000 1214540585