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: 192 384 576
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.
/**
* suppose 192
* 192x1=192
* 192x2=384
* 192x3=576
* 192384576
* no single digit should repeat
*/
import java.util.Scanner;
class fascinating
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
long m = Long.parseLong(String.valueOf(n)+String.valueOf(n*2)+String.valueOf(n*3));
int arr[] = new int[10];
while(m>0)
{
int r = (int)(m%10);
arr[r]++;
m=m/10;
}
boolean flag=false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>=2)
{
flag=true;
break;
}
}
if(!flag)
System.out.println(n+"is a fascinating number");
else
System.out.println(n+"is not a fascinating number");
}
}
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: 192 384 576
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.
/**
* suppose 192
* 192x1=192
* 192x2=384
* 192x3=576
* 192384576
* no single digit should repeat
*/
import java.util.Scanner;
class fascinating
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter a number: ");
int n = sc.nextInt();
long m = Long.parseLong(String.valueOf(n)+String.valueOf(n*2)+String.valueOf(n*3));
int arr[] = new int[10];
while(m>0)
{
int r = (int)(m%10);
arr[r]++;
m=m/10;
}
boolean flag=false;
for(int i=0;i<arr.length;i++)
{
if(arr[i]>=2)
{
flag=true;
break;
}
}
if(!flag)
System.out.println(n+"is a fascinating number");
else
System.out.println(n+"is not a fascinating number");
}
}