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.

Image is Unavailable

Algorithm

  1.   Start
  2. x:=0
  3. Print "Enter a number"
  4. Read x;
  5. count=0
  6. i=1
  7. if i<=9, go to step 8 or else go to step 24
  8. j=i
  9. a=Integer.toString(x*2)
  10. b=Integer.toString(x*3)
  11. c=Integer.toString(x)
  12. t=Integer.parseInt(c+a+b)
  13. digit:=0
  14. c=0
  15. If t>0, go to step 16 or else go to step 21
  16. digit=t%10
  17.  t=t/10
  18. if digit==j, go to step 19 or else go to step 20
  19. c++
  20. Go to step 15
  21. If c==1, go to step 22 or else go to step 23
  22. count ++
  23. i++. Go to step 7
  24. If count==9, go to step 25 or else go to step 26
  25. Print "The given number is fascinating number"
  26. Print "The given number is not a fascinating number"
  27. 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 NameDescription
 intTo accept a number from the user 
 String To store double of the number 
 String To store triple of the number 
 String To store string form of number 
 intTo parse a value to the function 
 intdigit To store the digits 
 intTo count the frequency of each digit 
 intcount To count the number of digits whose frequency is 1 
 intTo act as counter variable 
 intTo act as a temp variable 

Method Description

 Return TypeMethod NameDescription
 voidaccept() To accept data from the user. 
 int getNumber()To get the concatenated number 
 intcheckDigit(int) To check if a digit is present in the number or not. 
 voidisFascinating() To check whether the given number is fascinating number or not. 
 voidmain() To act as an entry gateway for the compiler 

Output

Image is Unavailable

Image is Unavailable





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
Pinterest: @ISCJavaCode
Facebook: @ISCJavaCode


Stay Healthy
Stay Happy
Stay Helpful


Comments

  1. please give the explanation of this program how this program will run.

    ReplyDelete

Post a Comment

You might also like:

Conversion of Complex Sentences to Simple Sentences

Conversion of Compound Sentences to Complex Sentences.

Conversion of Complex Sentences to Compound Sentences