# How To Check Whether a Number Is a Fascinating Number or Not in Java?

Fascinating number can be defined as a number which when multiplied with 2 and 3, and then the products after being concatenated with the number itself contains all the digits from 1 to 9.

For a number to be a fascinating number it should be three or more than three digit.

## To show you some instances

### Instance-1

Input number is 327

Let’s check it by using the logic of Fascinating number −

327 * 2 = 654
327 * 3 = 981


Concatenating “654” + “981” + “327” = 654981327

Hence, 327 is a Fascinating number.

### Instance-2

Input number is 192

Let’s check it by using the logic of Fascinating number −

192 * 2 = 384
192 * 3 = 576


Concatenating “384” + “576” + “192” = 384576192

Hence, 327 is a Fascinating number.

### Instance-3

Input number is 241

Let’s check it by using the logic of Fascinating number −

241 * 2 = 482
241 * 3 = 723


Concatenating “482” + “723” + “241” = 482723241

Hence, 241 is not a Fascinating number

Some other examples of fascinating numbers include 192, 1920, 2019, 327 etc.

## Algorithm

• Step 1 − Get an integer number either by initialization or by user input.

• Step 2 − Check if that is a three-digit number.

• Step 3 − Multiply the number by 2 and 3.

• Step 4 − Concatenate both the products with the number itself.

• Step 5 − Now find If all the digits from 1 to 9 are present in the number. If it does then it is a fascinating number

## Multiple Approaches

We have provided the solution in different approaches.

• By Using Static Input Value

• By Using User Defined Method

Let’s see the program along with its output one by one.

### Approach-1: By Using Static Input Value

In this approach, initialize an integer value in the program and then by using the algorithm we can check whether a number is a Fascinating number or not.

#### Example

public class Main {

public static void main(String args[]){
// Initialized an integer value
int num = 327;
System.out.println("Given number: "+num);

// Store the product of the numbers in the variables
int prod1 = num*2;
int prod2 = num*3;

// Concatenate the numbers
String concatNum = prod1+""+prod2+num;
// Boolean value to store the result
boolean flag = true;

// Loops from 1 to 9
for(char c = '1'; c <= '9'; c++)  {
// COunt holds the number of times a digit occurs
int count = 0;
//loop counts the frequency of each digit
for(int i = 0; i < concatNum.length(); i++)  {
char ch = concatNum.charAt(i);
//compares the character of concatNum with i
if(ch == c)
//increments the count by 1 if the specified condition returns true
count++;
}
// Checks if all the digits are present in the number
if(count > 1 || count == 0)  {
flag = false;
break;
}
}
// Prints the result
if(flag)
System.out.println("Fascinating number");
else
System.out.println("Not a fascinating number");
}
}


#### Output

Given number: 327
Fascinating number


### Approach-2: By Using User Defined Method

In this approach, initialize an integer value in the program and pass this number as a parameter in a user defined method then inside the method by using the algorithm we can check whether a number is a Fascinating number or not.

#### Example

public class Mai {
static boolean fascinatingNum(int num){
// Store the product of the numbers in the variables
int prod1 = num*2;
int prod2 = num*3;

// Concatenate the numbers
String concatNum = prod1+""+prod2+num;
// Boolean value to store the result
boolean flag = true;

// Loops from 1 to 9
for(char c = '1'; c <= '9'; c++)  {
// COunt holds the number of times a digit occurs
int count = 0;
//loop counts the frequency of each digit
for(int i = 0; i < concatNum.length(); i++){
char ch = concatNum.charAt(i);
//compares the character of concatNum with i
if(ch == c)
//increments the count by 1 if the specified condition returns true
count++;
}
// Checks if all the digits are present in the number
if(count > 1 || count == 0)  {
flag = false;
break;
}
}
return flag;
}
public static void main(String args[]){
// Initialized an integer value
int num = 327;
System.out.println("Given number: "+num);

// Calls the user defined method and stores the result in res variable
boolean res = fascinatingNum(num);

// Prints the result
if(res)
System.out.println("Fascinating number");
else
System.out.println("Not a fascinating number");
}
}


#### Output

Given number: 327
Fascinating number


In this article, we explored how to check a number whether it is a fascinating number or not in Java by using different approaches.