Fascinating Number Program in Java (ICSE/ISC)

⚡ Direct Answer & Key Takeaway

A Fascinating Number is an integer of three or more digits such that concatenating the number with its double (2 * n) and triple (3 * n) yields a 9-digit string containing every digit from 1 to 9 exactly once, with no zeros. For example, 192 gives 192, 384, and 576, forming 192384576. In Java, verify num >= 100, build concat = "" + num + (num * 2) + (num * 3), confirm length 9, and scan digits '1' through '9' using indexOf.

1. Why Concatenating n, 2n, and 3n Needs Length 9

A Fascinating Number relies on a 1-to-9 pandigital check across three scaled copies of the input: n, 2n, and 3n. Notice why the problem definition requires at least three digits (num >= 100): any 1-digit or 2-digit number produces at most 2 + 3 + 3 = 8 digits when multiplied by 1, 2, and 3, so it can never supply all nine distinct non-zero digits. Conversely, once num reaches 334 or higher, 3 * num crosses 1000, stretching the concatenated string to 10 or more characters. Even if a 10-character string contains every digit from 1 to 9, at least one digit repeats or a zero slips in. Checking concat.length() == 9 immediately eliminates both under-sized and over-sized inputs in constant time before any character scan begins.

  • Minimum 3 digits: num < 100 cannot reach 9 concatenated characters.
  • Upper limit for 3-digit Fascinating Numbers is 333 because 3 * 334 = 1002.
  • Exact length 9 is required so every digit 1 through 9 can appear strictly once.

2. Dry Run for 192, 273, and 999

Tracing three candidates reveals how quickly the length check and character scan separate valid inputs from invalid ones. Start with num = 192: doubleVal = 384 and tripleVal = 576, so concat becomes "192384576" with length 9. Iterating ch from '1' to '9' finds first == last at indices 0, 2, 3, 5, 6, 8, 7, 4, and 1 respectively—each digit occurs once, confirming 192 is Fascinating. Next, test num = 273: doubleVal = 546, tripleVal = 819, and concat = "273546819", which again has length 9 and hits every digit 1..9 cleanly. Finally, test num = 999: doubleVal = 1998 and tripleVal = 2997 yield "99919982997" of length 11. Because concat.length() == 9 is false, isFascinating stays false immediately without even entering the loop.

  • Trace 192: "192" + "384" + "576" = "192384576" (length 9, all 1..9 unique).
  • Trace 273: "273" + "546" + "819" = "273546819" (length 9, all 1..9 unique).
  • Trace 999: "999" + "1998" + "2997" = "99919982997" (length 11 -> rejected).

3. Detecting Duplicate Digits Without Arrays

Most ICSE students reach for a 10-element integer frequency array to verify unique digits, yet String.indexOf paired with String.lastIndexOf solves the same check cleanly without arrays. Inside the loop for (char ch = '1'; ch <= '9'; ch++), indexOf(ch) locates the leftmost occurrence of digit ch while lastIndexOf(ch) locates its rightmost occurrence. If first == -1, the digit is completely missing; if first != last, that digit appears at least twice. Whenever any digit fails either test, setting isFascinating = false and breaking exits early. Crucially, because concat is guaranteed to have exactly 9 characters and all 9 distinct digits '1' through '9' are verified present, no room remains for '0' by the pigeonhole principle, giving a compact, readable BlueJ solution.

  • first == -1 catches missing digits; first != last catches duplicate digits.
  • Pigeonhole guarantee: 9 unique digits from '1'..'9' in a 9-char string automatically exclude '0'.
  • Zero arrays needed, keeping the solution ideal for Class 10 String-handling units.

4. FascinatingNumber.java (BlueJ)

import java.util.Scanner;

public class FascinatingNumber {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter a number (3 or more digits): ");
        int num = sc.nextInt();

        if (num < 100) {
            System.out.println(num + " is not a Fascinating Number.");
            sc.close();
            return;
        }

        int doubleVal = num * 2;
        int tripleVal = num * 3;
        String concat = "" + num + doubleVal + tripleVal;

        boolean isFascinating = (concat.length() == 9);
        if (isFascinating) {
            for (char ch = '1'; ch <= '9'; ch++) {
                int first = concat.indexOf(ch);
                int last = concat.lastIndexOf(ch);
                if (first == -1 || first != last) {
                    isFascinating = false;
                    break;
                }
            }
        }

        System.out.println("Concatenated string: " + concat);
        if (isFascinating) {
            System.out.println(num + " is a Fascinating Number.");
        } else {
            System.out.println(num + " is not a Fascinating Number.");
        }
        sc.close();
    }
}

Verified Console / Dry-Run Output:

Enter a number (3 or more digits): 192
Concatenated string: 192384576
192 is a Fascinating Number.

Enter a number (3 or more digits): 273
Concatenated string: 273546819
273 is a Fascinating Number.

Enter a number (3 or more digits): 999
Concatenated string: 99919982997
999 is not a Fascinating Number.

5. Variable Description Table (VDT) — FascinatingNumber

Variable NameData TypePurpose in Program
scScannerReads the candidate integer from the user
numintStores the input number being checked
doubleValintStores num * 2 before string concatenation
tripleValintStores num * 3 before string concatenation
concatStringHolds the joined text of num, doubleVal, and tripleVal
isFascinatingbooleanTracks whether concat satisfies length 9 and unique digits 1 to 9
🎯 1-on-1 Live Mentorship & Free Study Kit

Sharpen String & Digit Logic with 4Achievers Junior

Master Java String methods, pandigital checks, and ISC practicals in 1-on-1 or 1:4 Micro-Pods at 4Achievers Junior (Grades 3-12).

💬 WhatsApp Now
🔒 Instant WhatsApp notes + free 60-min 1-on-1 trial session
Confirmed! Our Senior Mentor is sending your study kit & free 1-on-1 demo link on WhatsApp now.

6. Common Mistakes & Top Scorer Tips

  • Avoid Mistake: Adding num + doubleVal + tripleVal numerically instead of starting with "" to force string concatenation.
  • Avoid Mistake: Checking only that digits '1' to '9' exist without verifying length == 9 or uniqueness, which fails when duplicates or '0' appear.
  • Avoid Mistake: Forgetting the num < 100 check or failing to reject 4-digit numbers whose concatenation exceeds 9 digits.
  • Examiner / Mentor Tip: Know the four 3-digit Fascinating Numbers—192, 219, 273, and 327—for quick verification in BlueJ lab tests.
  • Examiner / Mentor Tip: Never forget the leading "" in "" + num + doubleVal + tripleVal, or Java will add the numbers arithmetically.
  • Examiner / Mentor Tip: Explain clearly in viva why checking concat.length() == 9 plus '1'..'9' uniqueness implicitly guarantees zero is absent.

7. Interactive Concept Check Quiz

Question: Which of the following 3-digit integers is a Fascinating Number?

8. Frequently Asked Questions

Which 3-digit numbers are Fascinating Numbers?
The complete list of 3-digit Fascinating Numbers is 192, 219, 273, and 327. Any 3-digit number above 333 produces a 4-digit triple (3 * n >= 1000), pushing concat past 9 characters.
Do we need a separate check for the digit '0' if concat.length() == 9 and '1'..'9' each appear once?
No. Because concat must have length 9 and contain every digit from '1' to '9' once, all 9 slots are filled by 1..9, making it impossible for '0' to appear.
Why does the expression start with "" + num + doubleVal + tripleVal?
Without the leading empty string "", Java performs integer addition (num + doubleVal + tripleVal = 6 * num) instead of joining the three numbers side by side as text.

Explore Related Lessons & Free Interactive Tools

Free 1-on-1 Session

Book a Free Demo Class