WAP in JAVA to check whether a number is unique number or not
A unique number has no repeatation of digits
Eg : 4056 is unique , 50905 is not unique
Answers
import java.util.*;
class unique_number
{
public static void main(String args[ ])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number to check whether it is unique number or not");
int n=sc.nextInt();
int cpy=n;
int d=0;
int c=0;
int flag=0;
for(int i=0;i<=9;i++)
{
n=cpy;
c=0;
while(n>0)
{
d=n%10;
if(d==i)
{
c++;
}
n=n/10;
}
if(c>1)
{
flag=1;
break;
}
}
if(flag==0)
{
System.out.println("Unique number");
}
else
{
System.out.println("Not a unique number");
}
}
}
---------------------------------------------------------------------------------
OUTPUT :
Enter a number to check whether it is unique number or not
689021
Unique number
--------------------------------------------------------------------------------------
Hope it helps :-)
____________________________________________________________________
class h;
{
public static void main(String args[])throws Exceptions
{
int n,r,m,s=0,f=0,i;
DataInputStream in=new DataInputStream(System.in);
System. out. println("Enter any number");
n=Integer.parseInt(in.readLine());
m=n;
for(i=0;i<=9;i++)
{
while(m>0)
{
r=m%10;
m=m/10;
if(r==1)
s±±;
}
if(s>1)
{
System. out. println("Not a unique number");
break;
}
else
f=1;
}
if(f==1)
System. out.println("Unique number ");
}
}
Hope it helps (。’▽’。)♡