Special Number in JavaIn 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 NumberIf 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 ExampleConsider 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
Let's implement the above logic in a Java program. Special Number Java ProgramSpecialNumberExample1.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 RangeSpecialNumberExample2.java Output: Enter the lower range: 1 Enter the upper range: 100000 1 2 145 40585
Next TopicPassing Array to Function In Java
