Here we will learn how to do programming on Special Number. We will start by understanding, what is Special Number and solve the following two questions on special Numbers in Java.

**Question**:**1. **Write a program in Java to check whether a number is Special or not.**2. **Write a Java Program to find all special numbers between the interval.

Also before we begin, if you do not know the working idea of for, while loop and if-else statement in java then click the link below and learn about them as this program uses them.

**What is a Special Number?**

A **special number **is a number whose sum of the factorial of its digits is equal to the original number. **Example:** 145 is a special number.

**Java Program to check whether a number is Special or not.**

To check for a special number, we take the number from the user, then separate the digits using the** % **operator, and then calculating the factorial of that individual digits. At last, add the digits and check if it’s equal to the

**original**

**number**and print the result accordingly as shown in the following program.

**Source 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 | import java.util.Scanner; class Main { public static void main(String[] args) { int digit, num, sum = 0, temp; Scanner in = new Scanner(System.in); System.out.println("Enter a number you want to check for special number:"); num = in .nextInt(); temp = num; // calculating sum of the factorial of its digit while (temp != 0) { digit = temp % 10; //calculating factorial of a number(digit) int fact = 1; for (int i = 2; i <= digit; i++) { fact = fact * i; } sum += fact; //calling factorial function temp = temp / 10; } //check and display the result if (sum == num) System.out.println(num + " is a Special number"); else System.out.println(num + " is Not a Special Number"); } } |

The **output** of special number in Java.

**Java Program to find all the special numbers between the interval.**

In this program, we have created a different function to calculate the sum of the digit and factorial of a number. The program takes two inputs from the user Lower Value and Upper-Value inputs and runs the loop for those values. Also if no special number is found between the upper and lower value, the program prints no value as shown in the program.

We have use boolean value to check if at-least one value is found or not and perform accordingly.

**Source 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import java.util.Scanner; class Main { public static void main(String[] args) { int lowerValue, upperValue; boolean checkSpecial = false; Scanner in = new Scanner(System.in); System.out.println("Enter the lowerValue and upperValue Number:"); lowerValue = in .nextInt(); upperValue = in .nextInt(); for (int i = lowerValue; i <= upperValue; i++) { //calling function called specialFunction() if (specialFunction(i)) { System.out.print(i + " "); checkSpecial = true; } } //display this if no special number are found if (!checkSpecial) System.out.println("Interval between " + lowerValue + " and " + upperValue + " has no special number"); } private static boolean specialFunction(int num) { int digit, sum = 0, temp; temp = num; // calculating sum of the factorial of its digit while (temp != 0) { digit = temp % 10; //calculating factorial of a number(digit) int fact = 1; for (int i = 2; i <= digit; i++) { fact = fact * i; } sum += fact; //calling factorial function temp = temp / 10; } return sum == num; } } |

The **output** of special number.