Fascinating Number in Java

In this section, we will learn what is a fascinating number and also create Java Program to check if the given number is fascinating or not. The fascinating number program is frequently asked in Java coding tests.

Fascinating Numbers

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:

  • If the given number is a 3 or more than three-digit
  • If the value getting after concatenation contains all digits from 1 to 9, exactly once.

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:

327×2=654

327×3=981

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

  1. First, check the given number consist of three digits or not. If no, print cannot be a fascinating number.
  2. Else, multiply the given number by 2 and 3, separately.
  3. Convert the results (from step 2) into a string.
  4. Concatenate the strings (from step 3) with the given number (n).
  5. Iterate over the string that we get after concatenation and count the frequency of each digit.
  6. Print “not a fascinating number” if any digit is missing or appeared multiple times. Else, print “fascinating number”.

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.

FascinatingNumberExample1.java

  1. import java.util.*;  
  2. public class FascinatingNumberExample1  
  3. {  
  4. public static void main(String args[])  
  5. {  
  6. int num, n2, n3;      
  7. Scanner sc=new Scanner(System.in);  
  8. System.out.print(“Enter any Number: “);  
  9. num = sc.nextInt();  
  10. n2 = num * 2;  
  11. n3 = num * 3;  
  12. //concatenating num, n2, and n3  
  13. String concatstr = num + “” + n2 + n3;  
  14. boolean found = true;  
  15. //checks all digits from 1 to 9 are present or not  
  16. for(char c = ‘1’; c <= ‘9’; c++)  
  17. {  
  18. int count = 0;  
  19. //loop counts the frequency of each digit  
  20. for(int i = 0; i < concatstr.length(); i++)  
  21. {  
  22. char ch = concatstr.charAt(i);  
  23. //compares the character of concatstr with i  
  24. if(ch == c)  
  25. //incerments the count by 1 if the specified condition returns true  
  26. count++;  
  27. }  
  28. //returns true if any of the condition returns true  
  29. if(count > 1 || count == 0)  
  30. {  
  31. found = false;  
  32. break;  
  33. }  
  34. }  
  35. if(found)  
  36. System.out.println(num + ” is a fascinating number.”);  
  37. else  
  38. System.out.println(num + ” is not a fascinating number.”);  
  39. }  
  40. }  

Output 1:

Enter any Number: 327
327 is a fascinating number.

Output 2:

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.

FascinatingNumberExample2.java

  1. import java.util.Scanner;  
  2. public class FascinatingNumberExample2  
  3. {  
  4. //function to check the Fascinating number  
  5. public static boolean isFascinatingNumber(int number)   
  6. {  
  7. int digit = 0;  
  8. //new number  
  9. String str = “” + number + number*2 + number*3;  
  10. //declaring an array  
  11. int digitarray[] = new int[10];  
  12.   
  13. //comparing array elements with characters of the string  
  14. for(int i=0; i<str.length(); i++)   
  15. {  
  16. //converts ith character into an integer  
  17. digit = str.charAt(i) – ‘0’;  
  18. //check arr[digit] element and ignore 0s  
  19. if(digit==0 || digitarray[digit]==0)  
  20. digitarray[digit]++;  
  21. else return false;  
  22. }  
  23. //checks the numbers that are missing  
  24. for(int i=1; i<digitarray.length; i++)   
  25. {  
  26. //digit i was not there in String  
  27. if(digitarray[i]==0)  
  28. return false;  
  29. }  
  30. //all conditions satisfied so, return true  
  31. return true;  
  32. }  
  33. //driver code  
  34. public static void main(String args[])   
  35. {  
  36. // declare variables  
  37. int lowerRange = 0, upperRange = 0;  
  38. //create Scanner class object to take input  
  39. Scanner scan = new Scanner(System.in);  
  40. System.out.print(“Enter lower range:”);  
  41. lowerRange = scan.nextInt();  
  42. System.out.print(“Enter upper range:”);  
  43. upperRange = scan.nextInt();  
  44. System.out.println(“The Fascinating number from “+ lowerRange + ” to “+ upperRange+” are: “);  
  45. //loop executes until the given condition returns false  
  46. for(int i=lowerRange; i<=upperRange; i++)   
  47. {  
  48. //calling user-defined number  
  49. if(isFascinatingNumber(i))  
  50. //prints all the fascinating numbers between a given range  
  51. System.out.print(i +” “);  
  52. }  
  53. }  
  54. }  

Output:

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

Leave a Comment