My FeedDiscussionsHeadless CMS
New
Sign in
Log inSign up
Learn more about Hashnode Headless CMSHashnode Headless CMS
Collaborate seamlessly with Hashnode Headless CMS for Enterprise.
Upgrade ✨Learn more

Program for Fascinating Number in JAVA

Rohit Kumar's photo
Rohit Kumar
·Jul 14, 2021·

2 min read

Fascinating Number :- The number of 3 digit or more, when we multiplied that number by 2 and 3, and product of both is concatenated with the original number, after concatenated the number contain all the digits from 1 to 9 exactly once, regardless of the number of zeros.

Example:

Consider a number 327 3271=327 3272=654 327*3=981

Concatenating all result: ‘327654981’

In this, it contains all the digits from 1 to 9 only once, so this is the fascinating number.

import java.until.*;
class FascinatingNumber
{
    boolean isUnique(String s)
    {
        int N[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency count of digit from 0 to 9
        int a, flag = 0;
        char ch;
        for(a=0; a<s.length(); a++)
        {
            ch = s.charAt(a);
            N[ch-48]++; 
            /*  increasing N[5] if ch='5' as '5'-48 = 53-48=5 
             *  (ASCII values of '0' to '9' are 48 to 57) */
        }

        for(a=1; a<10; a++)
        {
            //check every digit from '1' to '9' are present exactly once or not
            if(N[a]!=1)
            {
                flag = 1; //when frequency is not 1 then value of flag is 1
                break;
            }
        }

        if(flag == 1)
            return false;
        else
            return true;
    }
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        FascinatingNumber ob = new FascinatingNumber();

        System.out.print("Enter a number : ");
        int m = sc.nextInt();
        String q = Integer.toString(m); //converting the number to String

        if(q.length()<3)
            System.out.println("Number should be of atleast 3 digits.");

        else
        {
            String s = Integer.toString(m*1) + Integer.toString(m*2) + Integer.toString(m*3);
            /*  combine the multiple by converting them to Strings and concatenating them for checking the number is fascinating number or not*/
            if(ob.isUnique(s))
                System.out.println(m+" is a Fascinating Number.");
            else
                System.out.println(m+" is not a Fascinating Number.");
        }       
    }
}

OUTPUT:

Enter a number : 192 192 is a Fascinating Number.

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

Enter a number : 76 Number should be of atleast 3 digits.