Write a Program in Java to input a number and check whether it is a Unique Number or not.
Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.
import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n=Integer.parseInt(br.readLine());
String s=Integer.toString(n); //converting the number into String form
int l=s.length();
int flag=0;
/* loop for checking whether there are repeated digits */
for(int i=0;i<l-1;i++)
{
for(int j=i+1;j<l;j++)
{
if(s.charAt(i)==s.charAt(j)) //if any digits match, then we know it is not a Unique Number
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");
}
}
Note: A Unique number is a positive integer (without leading zeros) with no duplicate digits. For example 7, 135, 214 are all unique numbers whereas 33, 3121, 300 are not.
import java.io.*;
class UniqueNumber
{
public static void main(String args[])throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter any number : ");
int n=Integer.parseInt(br.readLine());
String s=Integer.toString(n); //converting the number into String form
int l=s.length();
int flag=0;
/* loop for checking whether there are repeated digits */
for(int i=0;i<l-1;i++)
{
for(int j=i+1;j<l;j++)
{
if(s.charAt(i)==s.charAt(j)) //if any digits match, then we know it is not a Unique Number
{
flag=1;
break;
}
}
}
if(flag==0)
System.out.println("**** The Number is a Unique Number ****");
else
System.out.println("**** The Number is Not a Unique Number ****");
}
}