What is Fascinating number
If a number of at least three digits is concatenated with its products by 2 and 3 and the result contains all the digits from 1 to 9 and these digits are present exactly once, then the number is said to be Fascinating.
It should be noted that there is NO restriction on the number of zeroes in the resultant number. Any number of zeroes may be present.

Example, Suppose the number is 192.
Now,192 * 2 = 384
192 * 3 = 576

Joining 192 with its products by 2 and 3, the resultant number is 192384576.
This number contains all digits from 1 to 9 and these digits appear exactly once in the result. Hence 192 is a fascinating number.

Note : The number should be of at least 3 digits to qualify for fascinating number test.

Following is the program to check for a fascinating number along with its explanation.

 static void methodTwo() { Scanner scanner = new Scanner(System.in); System.out.println("Enter a number"); int number = scanner.nextInt(); //convert the entered number to String format String numberInString = Integer.toString(number); if (numberInString.length() < 3) { System.out.println("Number should be atleast 3 digits"); } else { int twiceNumber = number * 2; int thriceNumber = number * 3; String numberToCheck = numberInString + Integer.toString(twiceNumber) + Integer.toString(thriceNumber); boolean isRepeated = false; // array of size 10 to store the number of times digits from 0 to // 9 occur in the number int digitArray[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; char[] characters = numberToCheck.toCharArray(); for (int i = 0; i < characters.length; i++) { char character = characters[i]; /* 48 is the ASCII code of 0, ch-48 gives the digit in * current comparison. Ex. if the digit is 2 then ch will * be 50, 50-48 will store the count of occurrence of 2 in * the number and so on */ digitArray[character - 48]++; } for (i = 1; i < 10; i++) { // checking if every digit from '1' to '9' are present // once or not if (digitArray[i] != 1) { // if any digit has a count more than 1 then set // boolean flag to true isRepeated = true; // terminate the loop as now we know that the // number is not fascinating break; } } if (isRepeated) { System.out.println("Number is not fascinating"); } else { System.out.println("Number is fascinating"); } } scanner.close(); }

Explanation

This method utilizes the fact that each character has an ASCII value. Numbers 0 to 9 have ASCII values in the range 48 to 57 where 0 has an ASCII value of 48, 1 has an ASCII value of 49 and so on.
Firstly the number to be checked is taken as input from the user. This number is then concatenated with its products by 2 and 3 and converted to a String.
This String is converted to a character array which is iterated to test the fascinating number.
Now in order to count the number of times each digit occurs in the number entered by user, we use an array of size 10 which is referred as digitArray.
Index 0 to 9 of digitArray corresponds to the digits 0 to 9 and the value stored at an index denotes the frequency of the occurrence of this digit.
That is, index 1 of the array denotes digit 1 and the value stored at index 1 shows how many times 1 occurs in the entered number.
Thus, during iteration over the characters (or digits) of the number to be checked, if the character (or digit) is 5, then its ASCII code (53) is subtracted from 48 and the number at this location in digitArrayis incremented.
That is,  digitArray[53-48]++ = digitArray++which means the value at index 5 now becomes 1.
Now if another 5 comes during iteration, then value at digitArraybecomes 2. This tells us that 5 occurs two times in the number.

After the iteration of characters in the number we are testing completes, a new iteration of digitArrayis performed from index 1 to 9.
Index 0 is skipped since we do not consider 0 while testing fascinating number. In each iteration, value at an index of digitArray is tested.
If it is not equal to 1, means either this digit is not present in the number or the digit occurs more than once. In both conditions, we are confirmed that the number is not a fascinating number and the loop is discontinued.

For any queries / feedback related to this program, feel free to comment in the space below.

0
Liked the article ? Spread the word...