Thursday, 8 April 2021

Fascinating Number in JAVA

What is Fascinating Number?

Given a number N, the task is to check whether it is fascinating or not. 

Fascinating Number: When a number( 3 digits or more ) is multiplied by 2 and 3, and when both these products are concatenated with the original number, then it results in all digits from 1 to 9 present exactly once. There could be any number of zeros and are ignored. 

Examples: 

 


Input: 192 

Output: Yes 

After multiplication with 2 and 3, and concatenating with original number, resultant number is 192384576 which contains all digits from 1 to 9.

Input: 853 

Output: No 

After multiplication with 2 and 3, and concatenating with original number, the resultant number is 85317062559. In this, number 4 is missing and the number 5 has appeared multiple times.



import java.util.*;
class Fascinating
{
boolean isFascinating(int n)
{
String num = String.valueOf(n) + String.valueOf(n*2) + String.valueOf(n*3);
int count;
for(char i='1';i<='9';i++)
{
count = 0;
for(int j=0;j<num.length();j++)
{
if(num.charAt(j)==i)
{
count++;
}
}
if(count != 1)
{
return false;
}
}
return true;
}
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
Fascinating obj = new Fascinating();
int num;
System.out.println("Enter number here : ");
num = in.nextInt();
if(obj.isFascinating(num))
{
System.out.println(num+" is Fascinating Number");
}
else
{
System.out.println(num+" is not a Fascinating Number");
}
}
}

No comments:

Post a Comment