Sunday, November 23, 2014

BlueJ Program On Unique Digit Number



In this program, user will enter a number and the program will check whether it is an unique digit number or not. Unique digit number is a number where all the digits are unique, e.g 123, 1234 etc

The first program uses an array



import java.io.*;
class UDigit
{
int i,no,x=0,arr[];
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
 System.out.print("\nEnter the number:");
 no=Integer.parseInt(br.readLine());
 arr=new int[10];
 while(no >0)
 {
   for(i=0;i< x;i++)
   {
        if(no%10==arr[i])
        break;
    }
    if(i< x)
    break;
    arr[x++]=no%10;
    no=no/10;
}
if(no >0)
System.out.println("\nNumber is not an unique digit number.");
else
System.out.println("\nNumber is an unique digit number.");
}

    public static void main(String args[])throws Exception
    {
       UDigit ob=new UDigit();
       ob.takeNumber();
        }
    }
  

Same Program Using Another Technique


import java.util.*;
class Arr
{
   int no,i,d;
   int a[]=new int[10];   
Scanner sc=new Scanner(System.in);
void show()
{
 
 System.out.print("\nEnter the number: ");
no=sc.nextInt();
if(uniqueChecking(no))
 System.out.print("\n"+no + " is Unique Digit Number");
 else
 System.out.print("\n"+no + " is not Unique Digit Number");
}
private boolean uniqueChecking(int n)
{
for(i=0;i<10;i++)
a[i]=0;
for(i=no;i>0;i=i/10)
{
    d=i%10;
    if(a[d]!=0)
    break;
    a[d]=d;
}
if(i==0) 
return true;
else
return false;
}
public static void main(String args[])
{
     Arr ob=new Arr();
     ob.show();
    }
}

    

The second program without any array

  
   import java.io.*;
class UDigit
{
int i,no,no1,no2,count;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
public void takeNumber() throws Exception
{
 System.out.print("\nEnter the number:");
 no=Integer.parseInt(br.readLine());
 no1=no;
 no2=no;
 while(no >0)
 {
  no1=no2;
  count=0;
  while(no1 >0)
  {
        if(no%10==no1%10)
       count++;
        no1=no1/10;
    }
    if(count >1)
    break;
    no=no/10;
}
if(no >0)
System.out.println("\nNumber is not an unique digit number.");
else
System.out.println("\nNumber is an unique digit number.");
}
    public static void main(String args[])throws Exception
    {
       UDigit ob=new UDigit();
       ob.takeNumber();
        }
    }


Related PostBlueJ Programs on Number
         

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner