Fascinating Number in Java

Write a program to input a positive integer and check if it is a fascinating number.

A fascinating number is one which when multiplied by 2 and 3, and then, after the results are concatenated with the original number, the new number contains all the digits from 1 to 9 exactly once.

For example, consider the number 192.
192 * 2 = 384
192 * 3 = 576
Concatenating both the results with the original number, we get:
“192” + “384” + “576” = 192384576.
Thus, 192 is a fascinating number.

219, 273, 327, 1902 are some more examples of fascinating numbers.

import java.io.*;
class Fascinating{
    public static void main(String args[])throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("N = ");
        int n = Integer.parseInt(br.readLine());
        int n2 = n * 2;
        int n3 = n * 3;
        String con = n + "" + n2 + n3;
        boolean found = true;
        for(char i = '1'; i <= '9'; i++){
            int count = 0;
            for(int j = 0; j < con.length(); j++){
                char ch = con.charAt(j);
                if(ch == i)
                    count++;
            }
            if(count > 1 || count == 0){
                found = false;
                break;
            }
        }
        if(found)
            System.out.println(n + " is a fascinating number.");
        else
            System.out.println(n + " is not a fascinating number.");
    }
}

Leave a Reply

%d bloggers like this: