Fascinating Number in Java

In this blog, we’ll look at the concept of fascinating number in java, dive into their fascinating properties. Learn how Java programming can help us uncover their hidden mysteries. This blog will inspire curiosity and imagination, whether you’re a math nerd, programmer, or just fascinated by numbers. Let’s delve into the world of fascinating numbers and uncover the secrets they hold in the realm of Java programming!

What is a Fascinating Number in Java?

Fascinating numbers are a unique class of numbers that exhibit an intriguing property when certain multiples of the number are concatenated together. To determine if a number is fascinating, the multiples of the number (2x, 3x, and 4x) are concatenated. If the resulting concatenation contains all the digits from 1 to 9 exactly once, it is considered a fascinating number.

Fascinating Numbers Examples:

1.  

192 :The multiples of 192 are 192×2 = 384, 192×3 = 576, and 192×4 = 768. If we concatenate these multiples together, we get 384576768. Notice that this concatenation contains all the digits from 1 to 9 exactly once, so 192 is a fascinating number.

2. 

101:A three-digit fascinating number is 101. When multiplied by 1, 2, and 3, and then concatenated, it forms the number 101202303, further exemplifying the captivating nature of fascinating numbers.

3. 

5832: The multiples of 5832 are 5832×2 = 11664, 5832×3 = 17496, and 5832×4 = 23328. Concatenating these multiples gives us 116641749623328, which contains all the digits from 1 to 9 once, making 5832 a fascinating number.

Get complete Java Programming Exercises and Solutions here!

Methods of Finding Whether to Check If a Number Is a Fascinating Number or Not in Java

To find fascinating numbers in Java, we can use different methods, such as using a loop and finding all fascinating numbers between a given range. Here’s an outline of the two approaches:

Fascinating Number in Java Using loop

import java.util.*;  
    public class FascinatingNumberExample1  
    {  
    public static void main(String args[])  
    {  
    int number, n2, n3;      
    Scanner sc=new Scanner(System.in);  
    System.out.print("Enter any Number: ");  
    number = sc.nextInt();  
    n2 = number * 2;  
    n3 = number * 3;  
    //concatenating num, n2, and n3  
    String concatstr = number + "" + 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(number + " is a fascinating number.");  
    else  
    System.out.println(number + " is not a fascinating number.");  
    }  
    }  

Explanation of the code:
The Java code provided is an example of how to discover intriguing numbers. The program asks the user to enter a number and then determines whether or not the number is interesting. The code explains the process as follows:

  • The program uses the Scanner class to accept user input when the user enters a number.
  • The number is multiplied by 2 and 3 to produce n2 and n3, which are two additional numbers. 
  • The program creates a single string called concatstr by concatenating the initial number, n2, and n3 together. 
  • It iterates through each digit from “1” to “9,” checking the frequency of each digit in the concatstr string, using a nested loop. 
  • The program sets the variable “found” to false if any digit is present only once or not at all, indicating that the number is not interesting.
  • The program then outputs whether or not the entered number is fascinating based on the value of the discovered variable.

Also, learn about Tech Number in Java, Now!

Output:

Enter any Number: 327
327 is a fascinating number.

Java program that finds all the fascinating numbers between the given range.

import java.util.Scanner;  
    public class FascinatingExample2  
    {  
    //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 +" ");  
    }  
    }  
    }  

Explanation of the code:

The given code is a Java program that checks for fascinating numbers within a given range. The program defines a function isFascinatingNumber() that takes an integer as input and determines if it is a fascinating number. 

The function creates a string by concatenating the number, the number multiplied by two, and the number by three in order to test for fascination. The program iterates through the string in order to convert each character to an integer. The function keeps track of how often each of the digits from 1 to 9 appears by using an array. The function returns true if the number is fascinating and all the digits appear exactly once without any zeros. If not, it returns false.

The program’s main() method prompts the user to enter the lower and upper range. The isFascinatingNumber() function is then invoked for every number as it loops through the range. If any fascinating numbers are found, the program prints them as output.

Know the Pronic Number in Java here!

Output:

Enter lower range:1
Enter upper range:1000
The Fascinating number from 1 to 1000 are: 
192 219 273 327 

Real-World Applications and Use Cases

A. Cryptography and Security Systems:

Fascinating numbers are used in cryptography and security systems to generate key generation, encryption algorithms, and secure data transmission protocols.

B. Mathematical Puzzles and Games:

In mathematical games and puzzles, users can be engaged and amused by utilizing intriguing numbers. They can serve as answers, hidden patterns, or components of games that are based on numbers.

C. Data Analysis and Pattern Recognition:

Researchers and data analysts can use fascinating numbers to uncover hidden patterns and correlations in datasets, helping to improve algorithms for data analysis and pattern recognition. 

Fascinating numbers have various applications such as identifying patterns and repetitions in DNA sequences, enhancing security measures, creating puzzles, and extracting valuable insights from complex datasets.

About The Author

Leave a Reply