Fascinating Number in Java

In this post, we will develop a Java program to check whether the given number is a Fascinating Number or not? We will also develop a Java program to find all fascinating numbers between two given numbers.

A number is called a fascinating number if it is (having at least 3 digits) multiplied by 2 and 3, and both these products are concatenated with the original number, then the new number contains all the digits from 1 to 9 exactly once. There could be any number of zeros and are ignored.

Example:-
Number = 192
Multiplying by 2 => 192 * 2 = 384
Multiplying by 3 => 192 * 3 = 576
Concatenation => “192” + “384” + “576” = 192384576, which contains all the digits from 1 to 9 exactly once. Hence, it is a fascinating number.

Since 192 is a fascinating number so, 1902, 19002, 19000…0002, are also a fascinating number. Because number of 0 is ignored.

Another example:-
Number = 273
Multiplying by 2 => 273 * 2 = 546
Multiplying by 3 => 273 * 3 = 819
Concatenation => “273” + “546” + “819” = “273546819”, which contains all the digits from 1 to 9 exactly once. Hence, it is a fascinating number.

Procedure

1) Take a number
2) Declare a string and initialize it with the concatenation of number, number*2, and number*3. For concatenation, we can use + operator

String str = "" + number + number*2 + number*3;

3) Declare an integer array of size 10, by default are all elements of an array is 0
4) Traverse through the characters of the string.

  • Find the ith character in ith iteration
  • Convert this character into digit (Not in ASCII value) and
  • Check array[digit] is 0 or not? If yes then increase the value of array[digit] element by 1. But if array[digit] is not 0 that means already it is increased due to previous existing same digit so, number is not a fascinating number.
If( arr[digit] == 0 ) arr[digit]++;
else return false;

5) Now, check the missing digit, ignore element=0. If any element of the array is zero then it means string (number) doesn’t contain that number. So, the number is not a fascinating number.
6) If all previous conditions are satisfied then it is a fascinating number.


Program

import java.util.Scanner;

public class FascinatingNumber {

   // Method to check the Fascinating number
   public static boolean isFascinating(int number){

      // declare variables
      int digit = 0;

      // new number
      String str = "" + number + 
                    number*2 + number*3;

      // declare an array of size 10
      int arr[] = new int[10];

      // compare array elements and
      // characters of the string
      for(int i=0; i<str.length(); i++) {

         // fetch ith character and 
         // convert it into number
         digit = str.charAt(i) - '0';

         // check arr[digit] element
         // and ignore 0
         if(digit==0 || arr[digit]==0)
            arr[digit]++;

         else return false;
      }

      // check their is any
      // missing number.
      // Ignore 1st element (0) of array
      for(int i=1; i<arr.length; i++) {

         // digit i was not their in String
         if(arr[i]==0)
            return false;
     }

      // all conditions satisfied so, return true
      return true;
   }

   // main method
   public static void main(String[] args) {

      // declare variables
      int number = 0;
      boolean result = false;

      //create Scanner class object to take input
      Scanner scan = new Scanner(System.in);

      // take input from end-user
      System.out.print("Enter an integer number::");
      number = scan.nextInt();

      // check number is Fascinating number or not
      result = isFascinating(number);

      // display
      if(result)
      System.out.println(number +
                  " is a Fascinating number.");
      else
      System.out.println(number +
                 " is not a Fascinating number.");

      // close Scanner class object
      scan.close();
   }
}

The Output for the different test-cases are:-

Enter an integer number::192
192 is a Fascinating number.

Enter an integer number::1902
1902 is a Fascinating number.

Enter an integer number::219
219 is a Fascinating number.

Enter an integer number::1234
1234 is not a Fascinating number.

Java program to find all fascinating number between two numbers

import java.util.Scanner;

public class FascinatingNumberInRange {

   // method to check the Fascinating number
   public static boolean isFascinating(int number) {

      // declare variables
      int digit = 0;

      // new number
      String str = "" + number + number*2 + number*3;

      // declare an array of size 10
      int arr[] = new int[10];

      // compare array elements and
      // characters of the string
      for(int i=0; i<str.length(); i++) {

         // fetch ith character and 
         // convert it into number
         digit = str.charAt(i) - '0';

         // check arr[digit] element
         // and ignore 0
         if(digit==0 || arr[digit]==0)
            arr[digit]++;

         else return false;
      }

      // check their is any
      // missing number.
      // Ignore 1st element (0) of array
      for(int i=1; i<arr.length; i++) {

         // digit i was not their in String
         if(arr[i]==0)
            return false;
      }

      // all conditions satisfied so, return true
      return true;
   }

   public static void main(String[] args) {

       // declare variables
       int minRange = 0, maxRange = 0;

       //create Scanner class object to take input
       Scanner scan = new Scanner(System.in);
       System.out.print("Enter minimum value of range:");
       minRange = scan.nextInt();
       System.out.print("Enter maximum value of range:");
       maxRange = scan.nextInt();

       System.out.println("The Fascinating number from "+ 
               minRange + " to "+ maxRange+" are: ");

       // loop
       for(int i=minRange; i<=maxRange; i++) {
           // check number
           if(isFascinating(i))
               System.out.print(i +" ");
       }

       // close Scanner class object
       scan.close();
    }
}

The Output for the different test-cases are:-

Enter minimum value of range:1
Enter maximum value of range:1000
The Fascinating number from 1 to 1000 are:
192 219 273 327

Enter minimum value of range:1000
Enter maximum value of range:10000
The Fascinating number from 1000 to 10000 are:
1902 1920 2019 2190 2703 2730 3027 3270

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *