Lucky Number

 //Write a program to accept a number and check it is a luck number or not.

//A number is said to be lucky if it does not contain any similar digit.

//ex- 9654 is a lucky number.

import java.util.*;

class lucky

{//class lucky opens

    public static void main(String[]args)

    {//main method opens

        Scanner sc=new Scanner(System.in);

        int r=0,count=0;//variable r to extract the digits,variable count to store the frequency 

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

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

        for(int i=0;i<=9;i++)//0//1//2//3//4

        {

            int k=n;//49654

            count=0;

            while(k>0)//9654>0 t//965>0 t//96>0//9>0 //0>0 false

            {

                r=k%10;//9654%10=4//965%10=5//96%10=6//9%10=9

                if(r==i)//4==4 t//5==4 f//6==4 f//9==4 f//4==4 t

                {

                    count=count+1;//1//1+1=2

                }

                k=k/10;//9654/10=965//965/10=96//96/10=9//9/10=0

            }

            if(count>1)//2>1 true

            {

                break;

            }

        }

        if(count>1)//2>1 true

        {

            System.out.println("It is not a lucky no");//49654

        }

        else

        System.out.println("It is a lucky 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 ...