Question

Check whether a number is Unique Number or not using recursion.

 

( A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 , 5243 are all unique numbers whereas 33, 3121, 200 are not.)

				
					ENTER THE NUMBER
123456
123456 IS  AN UNIQUE NUMBER
				
			

Share code with your friends

Share on whatsapp
Share on facebook
Share on twitter
Share on telegram

Code

				
					import java.util.Scanner;
public class UniqueNumber
{
    public static int CountDigits(long num,int digit,int noOfDigits)
    {
        if(num==0)
        {
            return noOfDigits; 
        }
        else
        {
            if(num%10==digit)
            {
                noOfDigits++;
            }
            return CountDigits(num/10,digit,noOfDigits);   
        }
    }

    public static void main()
    {
        int i=0,flag=0,count;
        long num=0;
        Scanner sc=new Scanner(System.in);
        System.out.println("ENTER THE NUMBER");
        num=sc.nextLong();
        System.out.println("Frequency of digits");

        for(i=0;i<=9;i++)
        {
            count=CountDigits(num,i,0);
            if(count>1)
            {
                flag=1; 
                break;
            }

        }
        if(flag==1)
        {
            System.out.println(num+" IS NOT AN UNIQUE NUMBER"); 
        }
        else
        {
            System.out.println(num+" IS  AN UNIQUE NUMBER"); 
        }
    }
}


				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *