Fascinating Number in Java
In this section, we will learn what is a fascinating number and also create Java programs to check if the given number is fascinating or not. The fascinating number program is frequently asked in Java coding tests.
Multiplying a number by two and three separately, the number obtained by writing the results obtained with the given number will be called a fascinating number. If the result obtained after concatenation contains all digits from 1 to 9, exactly once.
In other words, we can also say that a number (n) may be a fascinating number if it satisfies the following two conditions:
For example, 192, 1920, 2019, 327, etc. Let's understand the concept of fascinating numbers through an example.
Fascinating Number Example
Let's take any number (n) say 327 and check whether the given number is fascinating or not. On multiplying the given number (n) by 2 and 3, we get:
Now, concatenate the above results to the given number (n).
"327"+"654"+ "981"= 327654981
We observe that the resultant () contains all the digits from 1 to 9, exactly once. Hence, the given number 327 is a fascinating number. Note that, we have not added the result to the given number. Some other fascinating numbers are 192, 219, 273, 327, 1902, 1920, 2019 etc.
Steps to Find Fascinating Numbers
Let's implement the above steps in a Java program.
Fascinating Number Java Program
The base condition to check whether a number is fascinating or not is that the number must have at least 3 or more than three digits.
Enter any Number: 327 327 is a fascinating number.
Enter any Number: 8975 8975 is not a fascinating number.
Let's create another Java program that finds all the fascinating numbers between the given range.
Enter lower range: 1 Enter upper range: 10000 The Fascinating number from 1 to 10000 are: 192 219 273 327 1902 1920 2019 2190 2703 2730 3027 3270