Fascinating Number

 //Write a prigram to accept a number and check it is a fascinating number or not.

// A fascinating number is one which when multiplied by 2 and 3, and then after the results

//are concatenated with the original number, the new number contains all the digits from 1 to 9 

//exactly once. example 192

//192 x 1=192 ; 192 x 2=384 ; 192 x 3=576; now concatenating the result =192384576.

import java.util.*;

class fascinating

{//class fascinating opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        System.out.println("Enter a no");

        int n=sc.nextInt();//variable n to accept the no//192

        int n2=n*2;//variable n2 to store 2 times the no//192x2=384

        int n3=n*3;//variable n3 to store 3 times the no//192x3=576

        String con=n+""+n2+n3;//variable con to concatente the three nos//192384576

        boolean flag=true;//variable flag to store the result

        for(char i='1';i<='9';i++)//2

        {

            int c=0;//variable c to check the frequency

             

            for(int j=0;j<con.length();j++)//0

            {

                char ch=con.charAt(j);//1

                 

                if(ch==i)//9==1 f

                c++;//1

            }

            if(c>1 || c==0)//1>1 f || 1==0 f

            {

                flag =false;

                break;

            }

           

        }

        if(flag)

        System.out.println(n+" is a fascinating no");

        else

        System.out.println(n+" is not a fascinating no");

    }//main method closes

}//class closes


No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...