Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

ISC Practicals and Theory - Fascinating Number

Updated 21 Oct 2026

A Fascinating number is a 3-digit number which, when joined with its double and its triple, gives a 9-digit number that uses each of the digits 1 to 9 exactly once. This program, written in the main method alone, accepts a number from 100 to 999 and tells whether it is Fascinating.

Definition

Take a 3-digit number $n$ (from 100 to 999). Write $n$, $2n$ and $3n$ one after the other. If the joined number contains every digit from 1 to 9 exactly once and no zero, $n$ is a Fascinating number.

Example: $n = 192$. $2n = 384$ and $3n = 576$. Joined: $192384576$. The digits are 1, 9, 2, 3, 8, 4, 5, 7, 6, which are 1 to 9 once each, so 192 is Fascinating.

Example of a number that is not: $n = 100$. $2n = 200$, $3n = 300$. Joined: $100200300$ has zeros and repeats digits, so it is not Fascinating.

A number outside 100 to 999 is not accepted: the program prints INVALID INPUT.

Algorithm

  1. Accept the number n. If it is not a 3-digit number (below 100 or above 999), print INVALID INPUT and stop.
  2. Print n, 2n and 3n separated by commas.
  3. Set the boolean fas = true. If 3 * n > 999, then 2n or 3n has more than 3 digits and the joined number has more than 9 digits, so set fas = false.
  4. Otherwise create the array freq of size 10, where freq[d] counts how many times the digit d occurs. For k from 1 to 3, copy n * k into t and, while t > 0, add 1 to freq[t % 10] and drop the last digit with t = t / 10.
  5. A zero is not allowed, so if freq[0] is not 0, set fas = false. For every digit d from 1 to 9, freq[d] must be exactly 1; if not, set fas = false.
  6. If fas is true, print that n is a Fascinating number. Otherwise print that it is not.

Program

The array freq counts the digits of n, 2n and 3n in one pass of the k loop, and the final tests are made on the array.

import java.util.Scanner;

/* Program Fascinating checks whether a 3-digit number is Fascinating.
   A number n is Fascinating if n, 2n and 3n joined together use each of the
   digits 1 to 9 exactly once and contain no zero. */
public class Fascinating
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);          // reads input from the keyboard
        System.out.print("Enter a 3-digit number: ");
        int n = sc.nextInt();                         // number typed by the user
        if (n < 100 || n > 999)                       // must be a 3-digit number
            System.out.println("INVALID INPUT");
        else
        {
            System.out.println(n + ", " + (2 * n) + ", " + (3 * n));
            boolean fas = true;                       // stays true if n is Fascinating
            if (3 * n > 999)                          // 2n or 3n has more than 3 digits, so the joined number has more than 9 digits
                fas = false;
            else
            {
                int freq[] = new int[10];             // freq[d] counts how often digit d occurs
                for (int k = 1; k <= 3; k++)          // k = 1, 2, 3 gives n, 2n and 3n
                {
                    int t = n * k;                    // the multiple whose digits are counted
                    while (t > 0)
                    {
                        freq[t % 10]++;               // count the last digit
                        t = t / 10;                   // drop the last digit
                    }
                }
                if (freq[0] != 0)                     // a zero is not allowed
                    fas = false;
                for (int d = 1; d <= 9; d++)
                {
                    if (freq[d] != 1)                 // each digit 1 to 9 must occur once
                        fas = false;
                }
            }
            if (fas)
                System.out.println(n + " is a Fascinating number");
            else
                System.out.println(n + " is not a Fascinating number");
        }
    }
}

Variable description table

VariableData TypePurpose
argsString[]Command line arguments of main; not used
scScannerReads the input typed by the user
nintThe 3-digit number typed by the user
fasbooleanStays true while n can still be Fascinating
freqint[]freq[d] counts how often the digit d occurs in n, 2n and 3n together
kintMultiplier 1, 2, 3 that gives n, 2n and 3n
tintThe multiple n * k whose last digit is counted and dropped in each pass
dintLoop counter over the digits 1 to 9 in the final check

Sample input and output

Sample 1

Enter a 3-digit number: 192
192, 384, 576
192 is a Fascinating number

Sample 2

Enter a 3-digit number: 327
327, 654, 981
327 is a Fascinating number

Sample 3

Enter a 3-digit number: 100
100, 200, 300
100 is not a Fascinating number

Sample 4

Enter a 3-digit number: 50
INVALID INPUT

One value per line

Dry run

Input: $n = 192$. Here $3n = 576$, which is not more than 999, so the check continues. The loop on k takes the last digit repeatedly:

kt = n * kDigits taken from t (last digit first)freq entries increased
11922, 9, 1freq[2], freq[9], freq[1]
23844, 8, 3freq[4], freq[8], freq[3]
35766, 7, 5freq[6], freq[7], freq[5]

For example with t = 192: 192 % 10 = 2 so freq[2] becomes 1 and t = 19; 19 % 10 = 9 so freq[9] becomes 1 and t = 1; 1 % 10 = 1 so freq[1] becomes 1 and t = 0, and the loop stops.

Now freq[0] is 0, so the zero test passes. The loop d = 1 to 9 finds freq[d] equal to 1 every time, so fas stays true. The program prints 192, 384, 576 and 192 is a Fascinating number, the same as a real run.

Variations

  1. Fascinating numbers in a range: "Display all Fascinating numbers between m and n."
  2. Automorphic Number: "Accept a number and check whether it is an Automorphic number."
  3. Neon Number: "Accept a number and check whether it is a Neon number."
  4. Magic Number: "Accept a number and check whether it is a Magic number."