Fascinating Number | Write a program in Java to check whether the given number is fascinating number or not. | ISC Computer Science | ICSE Cmputer Application
Fascinating Numbers : Some numbers of 3 digits or more exhibit a very interesting property. The property is such that, when the number is multiplied by 2 and 3, and both these products are concatenated with the original number, all digits from 1 to 9 are present exactly once, regardless of the number of zeroes.
Algorithm
- Start
- x:=0
- Print "Enter a number"
- Read x;
- count=0
- i=1
- if i<=9, go to step 8 or else go to step 24
- j=i
- a=Integer.toString(x*2)
- b=Integer.toString(x*3)
- c=Integer.toString(x)
- t=Integer.parseInt(c+a+b)
- digit:=0
- c=0
- If t>0, go to step 16 or else go to step 21
- digit=t%10
- t=t/10
- if digit==j, go to step 19 or else go to step 20
- c++
- Go to step 15
- If c==1, go to step 22 or else go to step 23
- count ++
- i++. Go to step 7
- If count==9, go to step 25 or else go to step 26
- Print "The given number is fascinating number"
- Print "The given number is not a fascinating number"
- Stop
Program
Buffered Reader
import java.io.*;
class Fascinating
{
int x;
void accept() throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter a number");
x=Integer.parseInt(br.readLine());
}
int getNumber()
{
String a=Integer.toString(x*2), b=Integer.toString(x*3), c=Integer.toString(x);
return Integer.parseInt(c+a+b);
}
int checkDigit(int j)
{
int t=getNumber(), digit, c=0;
while(t>0)
{
digit=t%10;
t=t/10;
if(digit==j)
{
c++;
}
}
return c;
}
void isFascinating()
{
int count=0;
for(int i=1;i<=9;i++)
{
if(checkDigit(i)==1)
{
count++;
}
}
if(count==9)
{
System.out.println("The given number is fascinating number");
}
else
{
System.out.println("The given number is not a fascinating number");
}
}
void main() throws IOException
{
accept();
isFascinating();
}
}
Scanner
import java.util.*;
class Fascinating
{
int x;
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
x=sc.nextInt();
}
int getNumber()
{
String a=Integer.toString(x*2), b=Integer.toString(x*3), c=Integer.toString(x);
return Integer.parseInt(c+a+b);
}
int checkDigit(int j)
{
int t=getNumber(), digit, c=0;
while(t>0)
{
digit=t%10;
t=t/10;
if(digit==j)
{
c++;
}
}
return c;
}
void isFascinating()
{
int count=0;
for(int i=1;i<=9;i++)
{
if(checkDigit(i)==1)
{
count++;
}
}
if(count==9)
{
System.out.println("The given number is fascinating number");
}
else
{
System.out.println("The given number is not a fascinating number");
}
}
void main()
{
accept();
isFascinating();
}
}
Documentation
Variable Description
Data Type | Variable Name | Description |
int | x | To accept a number from the user |
String | a | To store double of the number |
String | b | To store triple of the number |
String | c | To store string form of number |
int | j | To parse a value to the function |
int | digit | To store the digits |
int | c | To count the frequency of each digit |
int | count | To count the number of digits whose frequency is 1 |
int | i | To act as counter variable |
int | t | To act as a temp variable |
Method Description
Return Type | Method Name | Description |
void | accept() | To accept data from the user. |
int | getNumber() | To get the concatenated number |
int | checkDigit(int) | To check if a digit is present in the number or not. |
void | isFascinating() | To check whether the given number is fascinating number or not. |
void | main() | To act as an entry gateway for the compiler |
Output
Share with all ISC Computer Science Students you know.
Comment below with all your program requests and blog suggestions.
Follow us on social media by clicking on these links.
Instagram : @IscJavaCode
Twitter: @IscCode
Quora: ISC Java Code
Stay Healthy
Stay Happy
Stay Helpful
please give the explanation of this program how this program will run.
ReplyDelete