FASCINATING NUMBER :
When a number( 3 digit or more) is multiplied by 2 and 3 ,and when 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.
Example:Joining 192 with its products by 2 and 3, the resultant number is 192384576. This number contains all digits from 1 to 9 and these digits appear exactly once in the result. Hence 192 is a fascinating number. Note : The number should be of at least 3 digits to qualify for fascinating number test
/* program to find whether it is fascinating number or not*/
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.");
}
}
}
OUTPUT:
Enter a number : 192
192 is a Fascinating Number.
Enter a number : 152
152 is not a Fascinating Number.
a function to find if a number is a fascinating number or not:
ReplyDeletestatic boolean checkFascinating(int n){
if((n+"").length()<3)return false;
String a=n+""+(n*2)+""+(n*3);
for(int i=1;i<=9;i++){
if(a.replace(i+"","").equals(a))return false;
}
return a.replace("0","").length()==9;
}
Get an Edge Titanium For Sale - TITIAN ART
ReplyDelete› titanium-art › titanium-art › titanium-art-Ti › titanium-art › titanium-art › titanium-art-Ti t fal titanium pan Ti titanium jewelry is the best premium stainless steel, an polished titanium alloy forged in-michère. The stainless steel edge is crafted with mens titanium wedding rings an outstanding design which will ensure an samsung galaxy watch 3 titanium