Question
Print Unique Number in given range 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.)
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 flag=0,count=0,j=0;
long lowerRange=0,upperRange=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("ENTER THE LOWER RANGE");
lowerRange=sc.nextLong();
System.out.println("ENTER THE UPPER RANGE");
upperRange=sc.nextLong();
for(i=lowerRange;i<=upperRange;i++)
{
flag=0;
for(j=0;j<=9;j++)
{
count=CountDigits(i,j,0);
if(count>1)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.print(i+" ");
}
}
}
}