ISC Practicals and Theory - Fascinating Number (OOP)
Updated 20 Oct 2026
This version writes the Fascinating number program as a class whose constructor stores the number and whose methods each do one job. A Fascinating number is a 3-digit number $n$ for which $n$, $2n$ and $3n$ joined together use the digits 1 to 9 exactly once.
Definition
Join $n$, $2n$ and $3n$ into one number. If it has the digits 1 to 9 once each and no zero, $n$ is Fascinating. The number $n$ must be a 3-digit number, from 100 to 999; any other input is INVALID INPUT.
Example: $192$, $384$, $576$ join to $192384576$, which uses 1 to 9 once each. For $100$ the joined number $100200300$ has zeros, so it is not Fascinating.
If $2n$ or $3n$ has more than 3 digits, the joined number has more than 9 digits and cannot be Fascinating. Otherwise it has exactly 9 digits, so it is enough to check that each of 1 to 9 occurs exactly once and 0 does not occur.
Algorithm
The numbers are never joined as text: the digits of $n$, $2n$ and $3n$ are counted one number at a time.
mainreadsnumand creates the object withnew Fascinating(num). The constructor stores it inn.isValid()returns true only for a number from 100 to 999. OtherwisemainprintsINVALID INPUT.- Otherwise
maincallsdisplay(), which prints the three numbers and callsisFascinating(). digitsIn(x)counts the digits ofx: it adds 1 tocountand doesx = x / 10whilex > 0.occurrences(x, d)counts how often the digitdoccurs inx: whilex > 0, ifx % 10equalsdit adds 1 toc, then doesx = x / 10.isFascinating():- returns false if
digitsIn(2 * n)ordigitsIn(3 * n)is more than 3 - for
dfrom 0 to 9, findstotal, the sum ofoccurrences(n, d),occurrences(2 * n, d)andoccurrences(3 * n, d) - returns false if
dis 0 andtotalis not 0, or ifdis 1 to 9 andtotalis not 1 - otherwise returns true
- returns false if
Program
The constructor stores the number, isValid checks it, digitsIn and occurrences count digits, isFascinating applies the test and display prints the result.
import java.util.Scanner;
/* Class Fascinating stores a 3-digit number and checks whether it is a
Fascinating number: n, 2n and 3n joined together use each of the
digits 1 to 9 exactly once and contain no zero. */
public class Fascinating
{
int n; // the 3-digit number to be checked
/* Constructor: stores the number.
Parameter: x - the number typed by the user. */
Fascinating(int x)
{
n = x;
}
/* Checks whether n has exactly three digits.
No parameters. Returns true if n is from 100 to 999, else false. */
boolean isValid()
{
return n >= 100 && n <= 999;
}
/* Counts the digits of a number.
Parameter: x - the number. Returns how many digits it has. */
int digitsIn(int x)
{
int count = 0; // digits counted so far
while (x > 0)
{
count++;
x = x / 10; // drop the last digit
}
return count;
}
/* Counts how many times a digit occurs in a number.
Parameters: x - the number, d - the digit (0 to 9). Returns its count. */
int occurrences(int x, int d)
{
int c = 0; // occurrences of d found so far
while (x > 0)
{
if (x % 10 == d) // last digit of x is d
c++;
x = x / 10; // drop the last digit
}
return c;
}
/* Checks whether n is a Fascinating number.
No parameters. Returns true if n, 2n and 3n together have 9 digits, no 0
and each digit 1 to 9 exactly once, else false. */
boolean isFascinating()
{
if (digitsIn(2 * n) > 3 || digitsIn(3 * n) > 3) // joined number would have more than 9 digits
return false;
for (int d = 0; d <= 9; d++)
{
int total = occurrences(n, d) + occurrences(2 * n, d) + occurrences(3 * n, d);
if (d == 0 && total != 0) // a zero is not allowed
return false;
if (d > 0 && total != 1) // each digit 1 to 9 exactly once
return false;
}
return true;
}
/* Displays n, 2n, 3n and the result of the check.
No parameters. Returns nothing. */
void display()
{
System.out.println(n + ", " + (2 * n) + ", " + (3 * n));
if (isFascinating())
System.out.println(n + " is a Fascinating number");
else
System.out.println(n + " is not a Fascinating number");
}
/* Reads a number, creates the object and calls its methods. */
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 num = sc.nextInt(); // number typed by the user
Fascinating ob = new Fascinating(num); // constructor stores the number
if (ob.isValid())
ob.display();
else
System.out.println("INVALID INPUT");
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
| n | int | Data member: the 3-digit number to be checked |
| x (in constructor) | int | Parameter of the constructor: the number typed by the user |
| x (in digitsIn) | int | Parameter: the number whose digits are counted; its last digit is dropped each time |
| count | int | Number of digits of x counted so far |
| x (in occurrences) | int | Parameter: the number in which a digit is searched |
| d (in occurrences) | int | Parameter: the digit to count |
| c | int | Number of times the digit d has been found in x |
| d (in isFascinating) | int | Loop counter for the digits 0 to 9 |
| total | int | How many times the digit d occurs in n, 2n and 3n together |
| args | String[] | Command line arguments of main (not used) |
| sc | Scanner | Reads the input typed by the user |
| num | int | The number typed by the user |
| ob | Fascinating | Object that holds the number and calls the methods |
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: 273
273, 546, 819
273 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: 1000
INVALID INPUTDry run
Input: $n = 192$. The constructor sets n = 192 and isValid() is true. In isFascinating(), digitsIn(384) is 3 and digitsIn(576) is 3, so neither is more than 3.
For example occurrences(192, 9): 192 % 10 = 2, not 9; x = 19; 19 % 10 = 9, so c = 1; x = 1; 1 % 10 = 1, not 9; x = 0 and the loop stops, returning 1.
| d | occurrences(192, d) | occurrences(384, d) | occurrences(576, d) | total | Test |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 | 0 | must be 0, passes |
| 1 | 1 | 0 | 0 | 1 | must be 1, passes |
| 2 | 1 | 0 | 0 | 1 | passes |
| 3 | 0 | 1 | 0 | 1 | passes |
| 4 | 0 | 1 | 0 | 1 | passes |
| 5 | 0 | 0 | 1 | 1 | passes |
| 6 | 0 | 0 | 1 | 1 | passes |
| 7 | 0 | 0 | 1 | 1 | passes |
| 8 | 0 | 1 | 0 | 1 | passes |
| 9 | 1 | 0 | 0 | 1 | passes |
Every test passes, so isFascinating() returns true. display() 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."