In this tutorial, we will write a program in Java to check for a Fascinating number. We will also write a program to display all the Fascinating numbers in a given range.
What is a Fascinating Number?
A number is called Fascinating number if it satisfies all the following conditions:
- Number contains at least three digits.
- When we multiply the number by 2 & 3 and concatenate the results with the original number then it contains all the digits from 1 to 9 exactly once.
For example, 219 is a Fascinating number.
219x2 = 438 219x3 = 657
Now concatenate these results with the original number and it becomes “219”+”438″+”658″ = 219438657. As you can see the after concatenation, we see all the numbers from 1 to 9 exactly once in the result.
Let’s take another example. 220 is not a Fascinating number.
220x2 = 440 220x3 = 660
Concatenating this produces the number “220”+”440″+”660″=220440660. It does not contain all the number from 1 to 9.
Steps to check Fascinating number
- First, we check if the input number contains minimum three digits, if it doesn’t have at least three digits, display “not a fascinating number” else proceed to next step.
- Multiply the original number by 2 and 3 separately and convert multiplication results into a String.
- Concatenate the both the strings with the string value of the original number.
- Run a loop from 1 to 9 over the concatenated string and count the frequency of each digit.
- Display “not a fascinating number” if any digit from 1 to 9 is missing or any digit appear multiple times, else display “is a fascinating number”.
Java Program to check if a number is Fascinating number
We are using the same steps that we have mentioned above, however the nested for loop can be a bit confusing so I will try to explain the logic:
In the following program, you can see that the outer for loop is running from 1 to 9. For each number(1 to 9), the inner for loop is running from the start of the string till the end of the string to count the occurrence of that number.
At any time if the counter shows 0 ( which means a number is missing) or greater than 1 (which means the number occurred more than once), we are using break statement to come out of the loop and display “not a fascinating number”.
import java.util.*; public class JavaExample { public static void main(String args[]) { int num, multiply2, multiply3; Scanner sc=new Scanner(System.in); System.out.print("Enter any Number: "); num = sc.nextInt(); multiply2 = num * 2; multiply3 = num * 3; //concatenating number, multiplication by 2 & 3 results String str = num + "" + multiply2 + multiply3; boolean flag = true; //checks the occurrence of 1 to 9 for(char ch = '1'; ch <= '9'; ch++) { int count = 0; //loop counts the frequency of each digit for(int i = 0; i < str.length(); i++) { char ch2 = str.charAt(i); //compares the character of str with the ch2 if(ch2 == ch) count++; } if(count > 1 || count == 0) { flag = false; break; } } if(flag) System.out.println(num + " is a fascinating number."); else System.out.println(num + " is not a fascinating number."); } }
Output:
Enter any Number: 219 219 is a fascinating number.
Output2:
Enter any Number: 450 450 is not a fascinating number.
Java Program to display Fascinating between a given range
This program displays all the fascinating numbers between a given range. The user is asked to enter the starting range and ending range, the program then checks all the numbers between this range (you can also use the logic of above program for checking the fascinating number) and displays those numbers that match the criteria of fascinating numbers.
import java.util.Scanner; public class JavaExample { public static boolean checkFascinatingNumber(int num) { int digit; String str = ""+num + num*2 + num*3; int oneToTenArray[] = new int[10]; for(int i=0; i<str.length(); i++) { digit = str.charAt(i) - '0'; if(digit==0 || oneToTenArray[digit]==0) oneToTenArray[digit]++; else return false; } for(int i=1; i<oneToTenArray.length; i++) { if(oneToTenArray[i]==0) return false; } return true; } public static void main(String args[]) { int startRange, endRange; Scanner scan = new Scanner(System.in); System.out.print("Enter Starting range:"); startRange = scan.nextInt(); System.out.print("Enter Ending range:"); endRange = scan.nextInt(); System.out.println("Fascinating number from "+ startRange + " to "+ endRange+" are: "); for(int i=startRange; i<=endRange; i++) { if(checkFascinatingNumber(i)) System.out.print(i +" "); } } }
Output:
Enter Starting range:1 Enter Ending range:500 Fascinating number from 1 to 500 are: 192 219 273 327
Leave a Reply