WAP in Java to check if a number is Unique or not
Answers
Answer:
import java.util.Scanner;
public class Unique_number
{
public static void main(String args[])
{
int n;int flag=1;
Scanner in=new Scanner(System.in);
System.out.println("Enter a number: ");
n=in.nextInt();
int c;
int no=n;
for(int i=0;i<=9;i++)
{
n=no;
c=0;
int d=n%10;
if(d==i)
{
c++;
n/=10;
}
if(c>1)
{
flag=0;
}
}
if(flag==0)
System.out.println("It is a Unique number");
else
System.out.println("It is not a Unique number");
}
}
Answer:-
A number is said to be unique if digits are not repeated.
Here is the program written in Java.
import java.util.*;
class Unique
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number: ");
int b=sc.nextInt();
int a=b,m=0,c=0,k=a;
while(a!=0)
{
r=a%10;
if(b!=0)
{
r1=b%10;
if(r1==r)
m=m+1;
b=b/10;
}
else
{
if(m>1)
c=1;
a=a/10;
b=k;
m=0;
}
}
if(c==1)
System.out.println("Not Unique. ");
else
System.out.println("Unique.");
}
}