Programming Examples

Java program for fascinating numbers


Write a Program in Java to input a number and check whether it is a Fascinating Number or not.

Fascinating Numbers: Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.

Let’s understand the concept of Fascinating Number through the following example:

Consider the number 192, 192 x 1 = 192 192 x 2 = 384 192 x 3 = 576

Concatenating the results : 192384576

It could be observed that ‘192384576’ consists of all digits from 1 to 9 exactly once. Hence, it could be concluded that 192 is a Fascinating Number.

Some examples of fascinating Numbers are : 192, 219, 273, 327, 1902, 1920, 2019 etc.

Sample Data:

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 atleast 3 digits.

Solution

import java.util.*;
class Fascinating
{
    public static void main(String args[])
    {
        Scanner sc=new Scanner(System.in);
        System.out.print("Enter any Number : ");
        int n = sc.nextInt();
        int n2 = n * 2;
        int n3 = n * 3;
        String con = n + "" + n2 + n3;
        boolean found = true;
        for(char i = '1'; i <= '9'; i++)
        {
            int count = 0;
            for(int j = 0; j < con.length(); j++)
            {
                char ch = con.charAt(j);
                if(ch == i)
                    count++;
            }
            if(count > 1 || count == 0)
            {
                found = false;
                break;
            }
        }
        if(found)
            System.out.println(n + " is a fascinating number.");
        else
            System.out.println(n + " is not a fascinating number.");
    }
}
Output

Enter any Number : 192

192 is a fascinating number.

Enter any Number : 273

273 is a fascinating number.

Enter any Number : 100

100 is not a fascinating number.

CCC Online Test 2021 CCC Practice Test Hindi Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Bank SSC Railway TET UPTET Question Bank career counselling in allahabad Sarkari Naukari Notification Best Website and Software Company in Allahabad Sarkari Exam Quiz