
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How To Check Whether a Number Is a Fascinating Number or Not in Java?
What is Fascinating Numbers?
A Fascinating number is a number if the result of concatenation of the (original) number with its multiples of 2 and 3 contains all the digits from 1 to 9.
For example, we have the number 192. Its product with 2 is 384, and with 3 is 576. Now concatenate (don't add) these with the original number: "192" + "384" + "576" = "192384576", which contains all the digits from 1 to 9 exactly once.
Here are some other fascinating numbers, suchas: 192, 1920, 2019, 327, etc.
Note: For a number to be a fascinating number, it should have three or more digits.
Input & Output Scenarios
Following are a few input and output scenarios that will help you to understand how to check fascinating numbers by doing mathematical calculations:
Scenario 1
Suppose the number is 327:
Input: 327 Output: Yes Calculation: Multiply with 2: 327 x 2 = 654 Multiply with 3: 327 x 3 = 981 Concatenate (not addition) the product with the number itself: 654 + 981 + 327 = 654981327
Since the result includes all digits from 1 to 9, 327 is a fascinating number.
Scenario 2
Suppose the given number is 241:
Input: 241 Output: No Calculation: Multiply with 2: 241 x 2 = 482 Multiply with 3: 241 x 3 = 723 Concatenate (not adding) the product with the number itself: 482 + 732 + 214 = 482732214
The result does not include all digits from 1 to 9; 214 is not a fascinating number.
Example 1
In the following example, we check if the number 327 is a Fascinating number. We multiply the number by 2 and 3 and concatenate (not addition) the product with the number itself, and if the result includes all the digits from 1 to 9 (by checking the frequency of each digit), we will display "Yes" or else "No":
public class checkFascinating { public static void main(String args[]){ int num = 327; System.out.println("The given number is: "+num); // the product of the numbers int prod1 = num * 2; int prod2 = num * 3; String concatNum = prod1 + "" + prod2 + num; boolean flag = true; for(char c = '1'; c <= '9'; c++) { int count = 0; for(int i = 0; i < concatNum.length(); i++) { char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) { count++; } } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } if(flag){ System.out.println("Yes! " + num + " is a fascinating number"); } else{ System.out.println("No! " + " is not a fascinating number"); } } }
Output
Following is the output of the above program:
The given number is: 327 Yes! 327 is a fascinating number
Example 2
This is another example of checking fascinating numbers. We define a method named checkFascinatingNumber(), which multiplies the number by 2 and 3, then concatenates (not addition) the original number with both results. If the combined result contains all digits from 1 to 9 exactly once, it is a Fascinating number; otherwise, no.
public class checkFascinating { public static void checkFascinatingNumber(int num){ // the product of the numbers int prod1 = num * 2; int prod2 = num * 3; String concatNum = prod1 + "" + prod2 + num; boolean flag = true; for(char c = '1'; c <= '9'; c++) { int count = 0; for(int i = 0; i < concatNum.length(); i++) { char ch = concatNum.charAt(i); //compares the character of concatNum with i if(ch == c) { count++; } } // Checks if all the digits are present in the number if(count > 1 || count == 0) { flag = false; break; } } if(flag){ System.out.println("Yes! " + num + " is a fascinating number"); } else{ System.out.println("No! " + num + " is not a fascinating number"); } } public static void main(String args[]){ int num = 54; System.out.println("The given number is: "+num); //calling the checkFascinatingNumber() method to check the fascinating number checkFascinatingNumber(num); } }
Output
Below is the output of the above program:
The given number is: 54 No! 54 is not a fascinating number