Fascinating Number in Java

In this section, we will explore the concept of fascinating numbers and develop Java programs to determine if a given number is fascinating or not. Checking whether a number is fascinating is a common question in Java coding assessments.

What are Fascinating Numbers?

A fascinating number is defined as a number that is obtained by separately multiplying the given number by two and three. When the results of these multiplications are written out and concatenated, the resulting number must contain all digits from 1 to 9 exactly once.

Another way to define a fascinating number (n) is by considering the following conditions:

  • The given number must have three or more digits.
  • When the results of multiplying the number by two and three separately are concatenated, the resulting value should contain all the digits from 1 to 9 exactly once.

For instance, numbers like 192, 1920, 2019, and 327 are examples of fascinating numbers. These examples help illustrate the concept of fascinating numbers.

Fascinating Number Example in Java

Let’s take a specific number, such as 327, and check if it is a fascinating number. By multiplying 327 by 2 and 3, we obtain:

327 × 2 = 654

327 × 3 = 981

Next, we concatenate the above results with the given number, 327:

“327” + “654” + “981” = 327654981

Upon examination, we can see that the resulting number contains all the digits from 1 to 9 exactly once. Therefore, the given number, 327, is a fascinating number. It is important to note that we did not add the result to the given number. Other examples of fascinating numbers include 192, 219, 273, 327, 1902, 1920, and 2019.

Steps to Find Fascinating Numbers

To determine if a number is a fascinating number, follow these steps:

  • Check if the given number consists of three digits. If it does not, print “Cannot be a fascinating number.
  • If the number has three digits, proceed by multiplying the given number by 2 and 3 separately.
  • Convert the results obtained from step 2 into strings.
  • Concatenate the strings from step 3 with the given number.
  • Iterate over the concatenated string and count the frequency of each digit.
  • If any digit is missing or appears multiple times, print “Not a fascinating number.
  • If all digits from 1 to 9 appear exactly once, print “Fascinating number.”

Fascinating Number Java Program

The fundamental requirement for determining whether a number is fascinating or not is that the number must consist of a minimum of three digits.

FascinatingNumberExample1.java

import java.util.*;  
public class FascinatingNumberExample  
{  
public static void main(String args[])  
{  
int num, n2, n3;      
Scanner sc=new Scanner(System.in);  
System.out.print("Enter any Number: ");  
num = sc.nextInt();  
n2 = num * 2;  
n3 = num * 3;  
//concatenating num, n2, and n3  
String concatstr = num + "" + n2 + n3;  
boolean found = true;  
//checks all digits from 1 to 9 are present or not  
for(char c = '1'; c <= '9'; c++)  
{  
int count = 0;  
//loop counts the frequency of each digit  
for(int i = 0; i < concatstr.length(); i++)  
{  
char ch = concatstr.charAt(i);  
//compares the character of concatstr with i  
if(ch == c)  
//incerments the count by 1 if the specified condition returns true  
count++;  
}  
//returns true if any of the condition returns true  
if(count > 1 || count == 0)  
{  
found = false;  
break;  
}  
}  
if(found)  
System.out.println(num + " is a fascinating number.");  
else  
System.out.println(num + " is not a fascinating number.");  
}  
}

Output 1:

Enter any Number: 327
327 is a fascinating number.

Output 2:

Enter any Number: 8975
8975 is not a fascinating number.

We will now create a Java program that identifies all the fascinating numbers within a given range.

FascinatingNumberExample2.java

import java.util.Scanner;  
public class FascinatingNumberExample1  
{  
//function to check the Fascinating number  
public static boolean isFascinatingNumber(int number)   
{  
int digit = 0;  
//new number  
String str = "" + number + number*2 + number*3;  
//declaring an array  
int digitarray[] = new int[10];  
  
//comparing array elements with characters of the string  
for(int i=0; i<str.length(); i++)   
{  
//converts ith character into an integer  
digit = str.charAt(i) - '0';  
//check arr[digit] element and ignore 0s  
if(digit==0 || digitarray[digit]==0)  
digitarray[digit]++;  
else return false;  
}  
//checks the numbers that are missing  
for(int i=1; i<digitarray.length; i++)   
{  
//digit i was not there in String  
if(digitarray[i]==0)  
return false;  
}  
//all conditions satisfied so, return true  
return true;  
}  
//driver code  
public static void main(String args[])   
{  
// declare variables  
int lowerRange = 0, upperRange = 0;  
//create Scanner class object to take input  
Scanner scan = new Scanner(System.in);  
System.out.print("Enter lower range:");  
lowerRange = scan.nextInt();  
System.out.print("Enter upper range:");  
upperRange = scan.nextInt();  
System.out.println("The Fascinating number from "+ lowerRange + " to "+ upperRange+" are: ");  
//loop executes until the given condition returns false  
for(int i=lowerRange; i<=upperRange; i++)   
{  
//calling user-defined number  
if(isFascinatingNumber(i))  
//prints all the fascinating numbers between a given range  
System.out.print(i +" ");  
}  
}  
}

Output:

Enter lower range: 1
Enter upper range: 10000
The Fascinating number from 1 to 10000 are: 
192 219 273 327 1902 1920 2019 2190 2703 2730 3027 3270

To gain further knowledge about Fascinating Numbers in Java, we encourage you to follow tutorials.freshersnow.com regularly.