Special Number in Java

Learn via video course
FREE
View all courses
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Java Course - Mastering the Fundamentals
Java Course - Mastering the Fundamentals
by Tarun Luthra
1000
5
Start Learning
Topics Covered

A number is considered a special number if the sum of the factorial of the digits in the number is equal to the number itself. The factorial of each digit in the number is calculated and added to a variable called sum. After the factorial of all the digits has been calculated, the sum is compared with the original number. If both are equal, the number is said to be a special number.

Introduction to Special number in Java

As programmers, we are familiar with many 'number-based' programs like Perfect Number, Armstrong Number, Magic Number, Happy Number, etc. Special Number is also one such program. Such programs revolve around various mathematical concepts and play around with the digits of the number.

A number is a special number if the sum of the factorial of the digits in the number is equal to the number itself. The factorial of each digit in the number is calculated and added to a variable called sum. After the factorial of all the digits has been calculated, the sum is compared with the original number. If both are equal, the number is said to be a special number.

Example: 145145 is said to be a special number. Since, 1!+4!+5!=1451! + 4! + 5! = 145, it is a special number. Calculations- 1!=11 != 1, 4!=244 != 24 and 5!=1205 != 120 after adding them, 1+24+1201 + 24 + 120 we get 145145. Hence, we can say that it is a special number.

introduction-to-special-number-in-java

There are 4 special numbers: 1, 2, 145, and 40585.

Steps to Find Special Number in Java

The following are the steps to find a special number-

  • Take input and initialize it as a number (n).
  • Split the number (n) into individual digits if the given number has more than one digit.
  • Find the factorial of all digits.
  • Create a variable sum(s) and sum up the factorial of each digit in this sum variable.
  • Compare the original number(n) with the sum(s).
  • If the sum is equal to the given number, the number(n) is said to be a special number or else not.

Code:

Output:

Java Program to Print all Special Numbers in the Range

All the special numbers in a given range can be printed with the help of the following program:

Output

Here we have globally declared an array of factorials from 0 to 9. Then we input the range from the user. Then each number in the range is checked to determine whether it is special or not. If there are no special numbers, print that there are no special numbers in the given range.

Conclusion

  • A number is said to be a special number if the sum of the factorial of the digits in the number is equal to the number itself.
  • The factorial of each digit in the number is calculated and is added to a variable called sum. After the factorial of all the digits has been calculated, the sum is compared with the original number. If both of them are equal, the number is said to be a special number.
  • There are 4 special numbers- 1, 2, 145 and 40585.