Fascinating Number Program in Java

Example: 327

Multiply with 2:
327×2=654

Multiply with 3:
327×3=981

Now, concatenate the all number.
“327”+”654″+ “981”= 327654981

Here we can see that all digits from 1 to 9 are available in concatenating string.


import java.util.Scanner;
public class FascinatingNumber
{

    public static void main(String args[])
    {
        int num, num2, num3;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter any Number: ");
        num = sc.nextInt();
        num2 = num * 2;
        num3 = num * 3;
        String concatNumbers = num + "" + num2 +""+ num3;
        System.out.println("Concated Number="+concatNumbers);
        boolean flag = true;
        for (char digit = '1'; digit <= '9'; digit++)
        {
            int count = 0;
            for (int i = 0; i < concatNumbers.length(); i++)
            {
                char ch = concatNumbers.charAt(i);
                if (ch == digit)
                {
                    count++;
                }
            }
            if (count > 1 || count == 0)
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            System.out.println("Fascinating Number.");
        } else
        {
            System.out.println("Not a Fascinating number.");
        }
    }
}


Output:

Enter number=23
Binary of 23=10111
Evil Number
learn java study java