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.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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:

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data

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.
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more