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
- Accept the number
n. If it is not a 3-digit number (below 100 or above 999), printINVALID INPUTand stop. - Print
n,2nand3nseparated by commas. - Set the boolean
fas = true. If3 * n > 999, then2nor3nhas more than 3 digits and the joined number has more than 9 digits, so setfas = false. - Otherwise create the array
freqof size 10, wherefreq[d]counts how many times the digitdoccurs. Forkfrom 1 to 3, copyn * kintotand, whilet > 0, add 1 tofreq[t % 10]and drop the last digit witht = t / 10. - A zero is not allowed, so if
freq[0]is not 0, setfas = false. For every digitdfrom 1 to 9,freq[d]must be exactly 1; if not, setfas = false. - If
fasis true, print thatnis 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
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the input typed by the user |
n | int | The 3-digit number typed by the user |
fas | boolean | Stays true while n can still be Fascinating |
freq | int[] | freq[d] counts how often the digit d occurs in n, 2n and 3n together |
k | int | Multiplier 1, 2, 3 that gives n, 2n and 3n |
t | int | The multiple n * k whose last digit is counted and dropped in each pass |
d | int | Loop 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 numberSample 2
Enter a 3-digit number: 327
327, 654, 981
327 is a Fascinating numberSample 3
Enter a 3-digit number: 100
100, 200, 300
100 is not a Fascinating numberSample 4
Enter a 3-digit number: 50
INVALID INPUTDry 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:
| k | t = n * k | Digits taken from t (last digit first) | freq entries increased |
|---|---|---|---|
| 1 | 192 | 2, 9, 1 | freq[2], freq[9], freq[1] |
| 2 | 384 | 4, 8, 3 | freq[4], freq[8], freq[3] |
| 3 | 576 | 6, 7, 5 | freq[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
- Fascinating numbers in a range: "Display all Fascinating numbers between m and n."
- Automorphic Number: "Accept a number and check whether it is an Automorphic number."
- Neon Number: "Accept a number and check whether it is a Neon number."
- Magic Number: "Accept a number and check whether it is a Magic number."