Google News
logo
Java program to Fascinating Number
A fascinating number is a number that, when multiplied by 2 and 3 and the products concatenated with the original number, results in a number that contains all digits from 1 to 9 exactly once.

For example, the number 192 is fascinating because 192 x 2 = 384, 192 x 3 = 576, and 192384576 contains all digits from 1 to 9 exactly once.

Here's a Java program to check if a number is fascinating or not :
Program :
import java.util.*;

public class FascinatingNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number: ");
        int num = sc.nextInt();
        int product1 = num * 2;
        int product2 = num * 3;
        String concat = num + "" + product1 + "" + product2;
        boolean fascinating = true;
        if (concat.length() != 9) {
            fascinating = false;
        } else {
            for (int i = 1; i <= 9; i++) {
                if (!concat.contains(i + "")) {
                    fascinating = false;
                    break;
                }
            }
        }
        if (fascinating) {
            System.out.println(num + " is a fascinating number.");
        } else {
            System.out.println(num + " is not a fascinating number.");
        }
    }
}
Output :
Enter a number: 192
192 is a fascinating number.
Enter a number: 197
197 is not a fascinating number.
In this program, we first take input from the user using the Scanner class.

We then compute the products of the number with 2 and 3, and concatenate them with the original number using string concatenation.

We then check if the concatenated string contains all digits from 1 to 9 exactly once, and print the result accordingly.