SUNDAY, 12 JULY 2026
Guide For School logo Guide For SchoolStudy Guide For Students On Java Programming
Physics | Chemistry | Mathematics
ICSE | ISC | CBSE
Guide For School logo Guide For SchoolICSE and ISC Resources

Fascinating Number Program in Java and Python

12 August 2015

Fascinating Number program with algorithm, explanation, Java solution and simple Python solution for ICSE and ISC students.

Question:

Write a program to input a number and check whether it is a Fascinating Number or not.

Fascinating Number: A number of three digits or more is called fascinating if, when the number is multiplied by 1, 2 and 3 and the three products are joined together, the resulting number contains all digits from 1 to 9 exactly once.

Example: 192 × 1 = 192 192 × 2 = 384 192 × 3 = 576 After joining: 192384576 The joined number contains all digits from 1 to 9 exactly once. Therefore, 192 is a Fascinating Number.

Algorithm:

Step 1: Start.

Step 2: Accept a number from the user.

Step 3: Convert the number to a string and check its length.

Step 4: If the number has less than three digits, display an appropriate message and stop.

Step 5: Multiply the number by 1, 2 and 3.

Step 6: Convert the three products to strings and join them into one string.

Step 7: Create an integer array of size 10 to store the frequency of digits 0 to 9.

Step 8: Read every character of the joined string and increase the frequency of the corresponding digit.

Step 9: Check the frequencies of digits 1 to 9.

Step 10: If every digit from 1 to 9 occurs exactly once, display that the number is fascinating; otherwise, display that it is not fascinating.

Step 11: Stop.

Explanation:

The program first checks whether the number has at least three digits, because fascinating numbers are considered only for numbers of three digits or more.

The products n × 1, n × 2 and n × 3 are converted to strings and joined. String joining is used because the program needs to examine every digit of the combined result.

The method isUnique() uses an integer array of size 10. Each array position stores the frequency of one digit. For example, index 5 stores how many times digit 5 occurs. In Java, the expression ch - '0' converts a digit character into its numeric index.

After counting all digits, the program checks array positions 1 to 9. If any digit from 1 to 9 does not occur exactly once, the number is not fascinating. If all these positions contain 1, the number is fascinating.

A fascinating number is tested by joining the number with its multiples, usually 2 times and 3 times the number. The combined string should contain each digit from 1 to 9 exactly once and should not contain 0. The program therefore builds a concatenated value and then checks digit frequencies. A digit-count array or repeated character checks can be used. The logic is not just multiplication; it is verifying whether the combined digits form a complete non-repeating set from 1 to 9.

Java Program:

Java
/**
* The class FascinatingNumber accepts a number and checks whether
* it is a Fascinating Number.
*
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/

import java.util.Scanner;

class FascinatingNumber
{
    boolean isUnique(String str)
    {
        /*
        * freq[0] stores the frequency of digit 0,
        * freq[1] stores the frequency of digit 1, and so on.
        */
        int freq[] = new int[10];

        /*
        * Count the frequency of every digit present in the joined string.
        */
        for(int i = 0; i < str.length(); i++)
        {
            char ch = str.charAt(i);
            int digit = ch - '0';
            freq[digit]++;
        }

        /*
        * A fascinating number must contain every digit from 1 to 9
        * exactly once in the joined result.
        */
        for(int i = 1; i <= 9; i++)
        {
            if(freq[i] != 1)
            return false;
        }

        return true;
    }

    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        FascinatingNumber ob = new FascinatingNumber();

        System.out.print("Enter a number: ");
        int n = sc.nextInt();

        String num = Integer.toString(n);

        if(num.length() < 3)
        {
            System.out.println("Number should be of at least 3 digits");
        }
        else
        {
            /*
            * Join the products of the number with 1, 2 and 3.
            */
            String joined = Integer.toString(n * 1)
            + Integer.toString(n * 2)
            + Integer.toString(n * 3);

            if(ob.isUnique(joined))
            System.out.println(n + " is a Fascinating Number");
            else
            System.out.println(n + " is not a Fascinating Number");
        }
    }
}

Equivalent Python Program:

Python
n = int(input("Enter a number: "))

# Fascinating numbers are considered for numbers of at least 3 digits.
num = str(n)

if len(num) < 3:
    print("Number should be of at least 3 digits")
else:
    # Join the products of the number with 1, 2 and 3.
    joined = str(n * 1) + str(n * 2) + str(n * 3)

    # freq[0] stores frequency of 0, freq[1] stores frequency of 1, etc.
    freq = [0] * 10

    # Count the frequency of every digit in the joined string.
    for ch in joined:
        digit = int(ch)
        freq[digit] = freq[digit] + 1

    flag = 0

    # Digits 1 to 9 must appear exactly once.
    for i in range(1, 10):
        if freq[i] != 1:
            flag = 1
            break

    if flag == 0:
        print(n, "is a Fascinating Number")
    else:
        print(n, "is not a Fascinating Number")

Output:

Enter a number: 273 273 is a Fascinating Number Enter a number: 853 853 is not a Fascinating Number Enter a number: 95 Number should be of at least 3 digits

Leave a Reply

Your email address will not be published. Comments are reviewed before appearing publicly.

Send a comment or correction

Study smarter

Everything you need for ICSE and ISC Computer

Programs, revision notes, solved papers and practical guidance—organized for quick study.

Browse all resources →