unique number in java
Answers
Question:-
- Write a program to check weather a number is unique or not
A number is said to be a unique number if the digits of the number are not repeated.
Program:-
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==b)
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.");
}
}
Answer:
this answer is very simple than the one who had already answered this question
Scanner sc=new Scanner (System.in) ;
int n=sc.nextInt() ;
int n1=n, d, a, c=0,k=0;
while(n! =0)
{
d=n%10;
while(n1! =0)
{
a=n1%10;
if(a==d)
c++;
n1/=10;
}
if(c>1)
{
k=1; //this statement will act as a flag variable
break;
}
else
c=0;
n/=10;
}
if(k==1)
System. out. println("Not Unique ") ;
else
System. out. println(" Unique ") ;