Question
Write a Program in Java to input a number and check whether it is a Unique Number or not.
( 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 a number :
12345
It 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.*;
public class UniqueNumber
{
public static void main(String args[] )
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number :");
int n=sc.nextInt();
int flag=0,i,temp=0,count=0,r=0;
for(i=0;i<=9;i++)
{
temp=n;
count=0;
while(temp>0)
{
r=temp%10;
if(r==i) /* if any digits are repeated, then it is not a UniqueNumber*/
{
count=count+1;
}
temp=temp/10;
}
if(count>1)
{
System.out.println(" It is not an UniqueNumber");
flag=1;
break;
}
}
if(flag ==0)
{
System.out.println("It is an Unique Number");
}
}
}