Example
Let’s consider the number 192:
- Multiply by 2: 192 × 2 = 384
- Multiply by 3: 192 × 3 = 576
- Concatenation: "192384576"
- The concatenated string contains all digits from 1 to 9 exactly once.
Since the condition is satisfied, 192 is a fascinating number.
Understanding the Logic Behind the Program
To write a program that finds fascinating numbers, we need to follow a systematic approach. Let’s take a look at a step-by-step breakdown of the logic:
Step 1: What Makes a Number Fascinating?
A number is fascinating if:
1. It is a 3-digit number (since multiplying a 2-digit number by 3 won’t give a 9-digit concatenated result).
2. When the number is multiplied by 2 & 3, & the results are concatenated with the original number, the final string contains all digits from 1 to 9 exactly once.
For example, take the number 192:
192 × 1 = 192
192 × 2 = 384
192 × 3 = 576
Concatenated result: 192384576
This string contains all digits from 1 to 9 without repetition or missing digits.
Step 2: Breaking Down the Program Logic
The program follows these steps:
1. Take Input Range: The user provides a starting & ending range.
2. Check Each Number: For every number in the range, check if it’s fascinating.
3. Concatenate Results: Multiply the number by 2 & 3, & concatenate the results with the original number.
4. Validate Digits: Ensure the concatenated string contains all digits from 1 to 9 exactly once.
5. Print Fascinating Numbers: If a number satisfies the above conditions, print it.
Step 3: Key Components of the Code
Let’s revisit the code & understand its key components:
A. isFascinating Function:
- This function checks if a number is fascinating.
- It multiplies the number by 2 & 3, concatenates the results, & validates the concatenated string.
- A `Set` is used to ensure there are no duplicate digits.
B. displayFascinatingNumbers Function:
- This function iterates through the given range & calls the `isFascinating` function for each number.
- If a number is fascinating, it’s printed.
C. Main Method:
- This is the entry point of the program.
- It takes user input for the range & calls the `displayFascinatingNumbers` function.
Step 4: Why Use a Set for Validation?
A `Set` is a collection that doesn’t allow duplicate elements. In the `isFascinating` function, we use a `Set` to store the digits of the concatenated string. If any digit is repeated, the `Set` will reject it, & the function will return `false`. This ensures that the concatenated string contains all digits from 1 to 9 exactly once.
Step 5: Handling Edge Cases
The program handles edge cases such as:
1. Numbers Outside the 3-Digit Range: Since fascinating numbers are typically 3-digit numbers, the program implicitly focuses on this range.
2. Invalid Inputs: If the user enters a negative range or a range where no fascinating numbers exist, the program will simply print nothing.
For Example:
Let’s have a look at an example to see how the program works:
Input:
Enter the starting range: 100
Enter the ending range: 200
Execution:
1. The program iterates through numbers from 100 to 200.
2. For each number, it calls the `isFascinating` function.
3. When it reaches 192, the function performs the following steps:
- 192 × 1 = 192
- 192 × 2 = 384
- 192 × 3 = 576
Concatenated result: 192384576
The program checks if this string contains all digits from 1 to 9 exactly once. Since it does, 192 is printed.
Output:
Fascinating numbers between 100 & 200 are:
192
Approach
We will use two approaches to check if a number is fascinating:
- Using a static input value
- Using a user-defined method
Approach 1: By Using Static Input Value
Steps to Follow:
- Multiply the given number by 2 and 3.
- Convert the original number and its multiples into a concatenated string.
- Check if the concatenated string contains all digits from 1 to 9 exactly once.
- If the condition is met, print that it is a fascinating number; otherwise, print that it is not.
Code:
public class FascinatingNumber {
public static void main(String[] args) {
int num = 192; // Static input
if (isFascinating(num)) {
System.out.println(num + " is a Fascinating Number.");
} else {
System.out.println(num + " is NOT a Fascinating Number.");
}
}
public static boolean isFascinating(int num) {
if (num < 100) {
return false; // Must be at least a 3-digit number
}
String concatenated = num + "" + (num * 2) + "" + (num * 3);
for (char digit = '1'; digit <= '9'; digit++) {
if (concatenated.indexOf(digit) == -1 || concatenated.lastIndexOf(digit) != concatenated.indexOf(digit)) {
return false;
}
}
return true;
}
}

You can also try this code with Online Java Compiler
Run Code
Output:
192 is a Fascinating Number.
Approach 2: By Using User-Defined Method
Steps to Follow:
- Take input from the user.
- Implement the logic inside a user-defined function.
- Check for all digits from 1 to 9 in the concatenated string.
- Return true if all digits are found exactly once.
Code:
import java.util.Scanner;
public class FascinatingNumberUserInput {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = scanner.nextInt();
scanner.close();
if (isFascinating(num)) {
System.out.println(num + " is a Fascinating Number.");
} else {
System.out.println(num + " is NOT a Fascinating Number.");
}
}
public static boolean isFascinating(int num) {
if (num < 100) {
return false; // Number must be at least 3 digits
}
String concatenated = num + "" + (num * 2) + "" + (num * 3);
for (char digit = '1'; digit <= '9'; digit++) {
if (concatenated.indexOf(digit) == -1 || concatenated.lastIndexOf(digit) != concatenated.indexOf(digit)) {
return false;
}
}
return true;
}
}

You can also try this code with Online Java Compiler
Run Code
Sample Output
Enter a number: 192
192 is a Fascinating Number.
Time and Space Complexity
Approach 1 (Static Input)
- Time Complexity: O(1) since the operations (multiplication, concatenation, and digit check) take constant time.
- Space Complexity: O(1) as only a few variables are used.
Approach 2 (User-Defined Method)
- Time Complexity: O(1) because the operations remain constant irrespective of input.
- Space Complexity: O(1) as the additional space used is minimal.
Frequently Asked Questions
What is a fascinating number?
A fascinating number is a three-digit or higher number where the concatenation of the number, its double, and its triple contains all digits from 1 to 9 exactly once.
Can a two-digit number be fascinating?
No, a fascinating number must have at least three digits.
What is the time complexity of checking a fascinating number?
The time complexity is O(1) since we are performing constant-time operations regardless of input size.
Conclusion
In this article, we discussed fascinating numbers in Java. We discussed what makes a number fascinating, provided examples, and implemented two approaches to check for fascinating numbers using Java. We also analyzed the time and space complexities of the approaches. Understanding fascinating numbers helps in improving logical thinking and problem-solving skills in programming.