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

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.

  1. main reads num and creates the object with new Fascinating(num). The constructor stores it in n.
  2. isValid() returns true only for a number from 100 to 999. Otherwise main prints INVALID INPUT.
  3. Otherwise main calls display(), which prints the three numbers and calls isFascinating().
  4. digitsIn(x) counts the digits of x: it adds 1 to count and does x = x / 10 while x > 0.
  5. occurrences(x, d) counts how often the digit d occurs in x: while x > 0, if x % 10 equals d it adds 1 to c, then does x = x / 10.
  6. isFascinating():
    • returns false if digitsIn(2 * n) or digitsIn(3 * n) is more than 3
    • for d from 0 to 9, finds total, the sum of occurrences(n, d), occurrences(2 * n, d) and occurrences(3 * n, d)
    • returns false if d is 0 and total is not 0, or if d is 1 to 9 and total is not 1
    • otherwise returns true

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

VariableData TypePurpose
nintData member: the 3-digit number to be checked
x (in constructor)intParameter of the constructor: the number typed by the user
x (in digitsIn)intParameter: the number whose digits are counted; its last digit is dropped each time
countintNumber of digits of x counted so far
x (in occurrences)intParameter: the number in which a digit is searched
d (in occurrences)intParameter: the digit to count
cintNumber of times the digit d has been found in x
d (in isFascinating)intLoop counter for the digits 0 to 9
totalintHow many times the digit d occurs in n, 2n and 3n together
argsString[]Command line arguments of main (not used)
scScannerReads the input typed by the user
numintThe number typed by the user
obFascinatingObject 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 number

Sample 2

Enter a 3-digit number: 273
273, 546, 819
273 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: 1000
INVALID INPUT

One value per line

Dry 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.

doccurrences(192, d)occurrences(384, d)occurrences(576, d)totalTest
00000must be 0, passes
11001must be 1, passes
21001passes
30101passes
40101passes
50011passes
60011passes
70011passes
80101passes
91001passes

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

  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."