0% found this document useful (0 votes)
38 views

Fascinating Numbers: Some Numbers of 3 Digits or More Exhibit A Very Interesting Property

The program takes a 3 digit or more number as input and checks if it is a fascinating number. A number is fascinating if when multiplied by 2 and 3, and those products are concatenated with the original number, all digits from 1 to 9 appear exactly once regardless of zeros. The program converts the number and its multiples to strings, concatenates them, then checks the string for unique digits to determine if the number is fascinating or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Fascinating Numbers: Some Numbers of 3 Digits or More Exhibit A Very Interesting Property

The program takes a 3 digit or more number as input and checks if it is a fascinating number. A number is fascinating if when multiplied by 2 and 3, and those products are concatenated with the original number, all digits from 1 to 9 appear exactly once regardless of zeros. The program converts the number and its multiples to strings, concatenates them, then checks the string for unique digits to determine if the number is fascinating or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Question:

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.
Lets 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.
/**
* The class FascinatingNumber inputs a number and checks if it a
Fascinating Number or not
* @author : www.guideforschool.com
* @Program Type : BlueJ Program - Java
*/
import java.util.*;
class FascinatingNumber
{
boolean isUnique(String q)
{
int A[] = {0,0,0,0,0,0,0,0,0,0}; //to store frequency of every
digit from '0' to '9'
int i, flag = 0;
char ch;
for(i=0; i<q.length(); i++)
{
ch = q.charAt(i);
A[ch-48]++;
/* increasing A[5] if ch='5' as '5'-48 = 53-48=5
* (ASCII values of '0' to '9' are 48 to 57) */
}
for(i=1; i<10; i++)
{
//checking if every digit from '1' to '9' are present exactly
once or not
if(A[i]!=1)
{
flag = 1; //flag is set to 1 if frequency is not 1
break;
}
}
if(flag == 1)
return false;
else
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 p = Integer.toString(n); //converting the number to String
if(p.length()<3)
System.out.println("Number should be of atleast 3 digits.");
else
{

String s = Integer.toString(n*1) + Integer.toString(n*2) +


Integer.toString(n*3);
/* Joining the first, second and third multiple of the number
* by converting them to Strings and concatenating them*/
if(ob.isUnique(s))
System.out.println(n+" is a Fascinating Number.");
else
System.out.println(n+" is not a Fascinating Number.");
}
}
}

You might also like