Fascinating Number – Java Program

Write a Program in Java to input a number and check whether it is a Fascinating Number or not.

Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.

Let’s understand the concept of Fascinating Number through the following example:

Consider the number 192,
192 x 1 = 192
192 x 2 = 384
192 x 3 = 576

Concatenating the results : 192384576

It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once.

Hence, it could be concluded that 192 is a Fascinating Number.

Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.

Sample Data:

Enter a number : 273
273 is a Fascinating Number.

Enter a number : 853
853 is not a Fascinating Number.

import java.util.*;
class FASC
{
    public static void main(String args[])
    {  
       Scanner sc = new Scanner(System.in);
       System.out.println("ENTER A NUMBER");
       int n = sc.nextInt();
       int num = n;
       int a = n*2;
       int b = n*3;
       String x = ""+num+a+b;
       System.out.println(x);
       
       int flag=1; //intially assume your number to be fascinating
       for(int i=1; i<=9; i++)
       {
           int c=0; //counter variable
           for(int j=0; j<x.length(); j++)
           {
              char ch = x.charAt(j);
              int temp = Character.getNumericValue(ch); //convert char to int
                        
              if(i==temp)
                 c++;
           }
           if(c!=1)
           {
               flag=0; //setting flag to 0 means number is not fascinating
               break;
           }
       }
        
       if(flag==1)//if flag is still 1
       {
           System.out.println("FASCINATING");
       }
       else
       {
           System.out.println("NOT FASCINATING");
       }
    }
}

Leave a Comment

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