In the previous article, we have discussed Java Program to Display Alternate Prime Numbers
In this article we are going to understand what Fascinating number is and how we can check whether a number is Fascinating or not in Java with examples.
Program to Check Fascinating Number
Fascinating numbers are numbers containing 3 or more digits, which when multiplied with 2 and 3 and after concatenated has all digits from 1 to 9 occurring once.
Example :
- 327: 327 * 2 = 654, 327 * 3 = 981, Concatenating(327,654,981)=327654981 It has all the digits from 1 to 9, hence 327 is a fascinating number.
- 192: 192*2 = 384, 192 * 3 = 576, Concatenating(192,384, 576)=192384576 It all the digits from 1 to 9, hence 192 is a fascinating number.
- 200: 200 * 2 = 400, 200 *3 = 600, Concatenating(200,400,600) = 200400600 It does not have all the digits from 1 to 9, hence 200 is not a fascinating number.
In the above examples the numbers 327 and 192 are only Fascinating numbers. 200 is not a Fascinating number.
Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.
Approach :
- We ask the user to enter a number or declare a number.
- It is checked if the number has 3 or more digits. If yes then multiply the number by 2 and 3 separately then concatenate all the three numbers into a string.
- Iterate that string and check if it has all digits from 1-9. If it has then it is said to be a fascinating number.
Let’s see different ways to check fascinating number.
Method-1: Java Program to Check Fascinating Number By User Input Value
import java.util.Scanner; public class FascinatingNumber { public static void main(String args[]) { //A number declared int num = 192; int prod2=0, prod3=0; String str; if(digitsCount(num)) { prod2 = num * 2; prod3 = num * 3; // Concatenates the numbers into a string str = num + "" + prod2 + prod3; // Calls the function to check if all the digits from 1 to 9 are present if(digitsCheck(str)) { System.out.println(num+" is a Fascinating Number"); } else { System.out.println(num+" is Not a Fascinating Number"); } } else { System.out.println(num+" is Not a Fascinating Number"); } } // Checks whether all digits from 1 to 9 are there in the string static boolean digitsCheck(String str) { for(char c = '1'; c <= '9'; c++) { int count = 0; for(int i = 0; i<str.length(); i++) { char ch = str.charAt(i); if(ch == c) count++; } if(count>1||count==0) return false; } return true; } // Function that checks whether the number has more than 3 digits or not static boolean digitsCount(int num) { int digits = 0; while(num>0) { num/=10; digits++; } if(digits>=3) return true; else return false; } }
Output: 192 is a Fascinating Number
Method-2: Java Program to Check Fascinating Number By User Defined Method
import java.util.Scanner; public class FascinatingNumber { public static void main(String args[]) { //Taking the number as input from the user using scanner class Scanner scan = new Scanner(System.in); System.out.print("Enter a number : "); int num = scan.nextInt(); int prod2=0, prod3=0; String str; if(digitsCount(num)) { prod2 = num * 2; prod3 = num * 3; // Concatenates the numbers into a string str = num + "" + prod2 + prod3; // Calls the function to check if all the digits from 1 to 9 are present if(digitsCheck(str)) { System.out.println(num+" is a Fascinating Number"); } else { System.out.println(num+" is Not a Fascinating Number"); } } else { System.out.println(num+" is Not a Fascinating Number"); } } // Checks whether all digits from 1 to 9 are there in the string static boolean digitsCheck(String str) { for(char c = '1'; c <= '9'; c++) { int count = 0; for(int i = 0; i<str.length(); i++) { char ch = str.charAt(i); if(ch == c) count++; } if(count>1||count==0) return false; } return true; } // Function that checks whether the number has more than 3 digits or not static boolean digitsCount(int num) { int digits = 0; while(num>0) { num/=10; digits++; } if(digits>=3) return true; else return false; } }
Output: Case-1 Enter a number : 192 192 is a Fascinating Number Case-2 Enter a number : 64 64 is Not a Fascinating Number
Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.
Related Java Programs: