In this java program tutorial, we will learn about Fascinating Number and its implementation through Java. We will check whether the number Fascinating Number or not and also another program to find the find Fascinating Number within a given range in java.
What is Fascinating Number?
A number is said to be a fascinating number if it is (having at least 3 digits) multiplied by 2 and 3, and then both these product’s results are concatenated with the original number, then the new number contains all the digits from 1 to 9 exactly once. There could be any number of zeros and are ignored.
Example:
Consider a Number = 192
Then multiply it by two and 3
192 × 2 = 384
192 × 3 = 576
Concatenating the above two numbers with the original number, we get:
“192” + “384” + “576” = 192384576
= result.
Now the final result contains all the digits from 1 to 9. Hence it is a Fascinating Number.
Procedure for Fascinating Number
- First, check if the entered/given number has three digits or not. If not, print “cannot be a Fascinating Number”.
- Else, Multiply the given number with 2 and 3.
- Then convert the product result into a string and Concatenate those strings along with the original number.
- Iterate the string that we get after concatenation and also keep the frequency count of the digits.
- Print “Not a Fascinating Number” if any of the digits (1 to 9) is missing or repeated.
- Else, print “It is a Fascinating Number”.
Java Program for Fascinating Number
1. Write a Java program to check whether a number is fascinating or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | import java.util.*; public class FascinatingNumber { public static void main(String args[]) { int num, product2, product3; Scanner sc = new Scanner(System.in); System.out.print("Enter 3 digit Number: "); num = sc.nextInt(); //multiplying with 2 and 3 product2 = num * 2; product3 = num * 3; //concatenating all three numbers String concatstr = num + "" + product2 + product3; boolean isfound = true; //checking for all the digits for (char c = '1'; c <= '9'; c++) { int count = 0; //counting the frequency for (int i = 0; i < concatstr.length(); i++) { char ch = concatstr.charAt(i); //comparing if (ch == c) count++; } //returns true if any of the condition returns true if (count > 1 || count == 0) { isfound = false; break; } } if (isfound) System.out.println(num + " is a fascinating number."); else System.out.println(num + " is not a fascinating number."); } } |
Output:
Enter 3 digit Number: 327
327 is a fascinating number.
//Another Output
Enter 3 digit Number: 145
145 is not a fascinating number.
2. Java Program to find all the fascinating numbers between the given Range.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | import java.util.Scanner; public class FascinatingNumberForRange { //separate method to do the calculation public static boolean isFascinatingFunc(int number) { int digit = 0; //multiplying and concatinating the numbers String str = "" + number + number *2 + number * 3; // declare an array of size 10 int arr[] = new int[10]; // comparing array elements with the string Characters for (int i = 0; i < str.length(); i++) { digit = str.charAt(i) - '0'; // ignoring the 0 character if (digit == 0 || arr[digit] == 0) arr[digit]++; else return false; } // check for the missing number, ignore 0 for (int i = 1; i < arr.length; i++) { if (arr[i] == 0) return false; } return true; } public static void main(String[] args) { // declare variables int lrRange = 0, upRange = 0; Scanner scan = new Scanner(System.in); //user Input System.out.print("Enter the Lower range: "); lrRange = scan.nextInt(); System.out.print("Enter the Upper range: "); upRange = scan.nextInt(); System.out.println("The Fascinating number between " + lrRange + " to " + upRange + " are: "); // looping for the range for (int i = lrRange; i <= upRange; i++) { // check number if (isFascinatingFunc(i)) System.out.print(i + " "); } } } |
Output:
Enter the Lower range: 1
Enter the Upper Range: 1000
The Fascinating number between 1 to 1000 are:
192 219 273 327