BlueJ Program on unique digit integer
The program is as follows:
A unique digit integer is a positive integer (without leading zeros) with no
duplicate digits. For example 7, 135,214 are all unique digit integers whereas 33,3121,300 are not.
Given two positive integers, m and n where m < n, write a program to determine how many unique digit integers are there in the range m and n (both inclusive)and display them.
Sample Data
Input:
m=100
n=120
Output:
The unique digit integers are:
102,103,104,105,106,107,108,109,120
Frequency of unique digit integers is : 9
import java.io.*;
class Digit
{
int k,x,i,j,m,n,c,arr[],index;
BufferedReader br;
public static void main(String args[])throws IOException
{
Digit ob=new Digit();
ob.take();
ob.show();
}
Digit()
{
br=new BufferedReader(new InputStreamReader(System.in));
arr=new int[1000];
c=0;
index=0;
}
public void take()throws IOException
{
while(true)
{
System.out.println("Enter the Lower range:");
m=Integer.parseInt(br.readLine().trim());
System.out.println("Enter the Upper range:");
n=Integer.parseInt(br.readLine().trim());
if(m < n)
break;
}
}
void show ()
{
System.out.println("The unique digit integers are:");
for(i=m;i < =n;i++)
{
index=0;
for(j=i;j > 0;j=j/10)
{
x=j%10;
for(k=0;k < index;k++)
{
if (arr[k]==x)
break;
}
if(k < index)
break;
else
arr[index++]=x;
}
if (j==0)
{
c++;
System.out.print(" "+i);
}
}
System.out.println("\nFequency of unique digit integers is :"+ c);
}
}
Variable Descriptions of the unique digit integer program
‘m’ – This will hold the lower range
‘n’ – it will hold the upper range
‘i’, ‘j’, ‘k’ are loop control variables
‘x’ is used to hold digits
‘c’ is the counter to count unique digit numbers
‘arr[]’ is used to check the numbers for unique digits
‘index’ is the array index of the array ‘arr[]’
‘br’ BufferedReader class object
Algorithm of the unique digit integer program
Step 1: Take the lower range and store in ‘m’
Step 2: Take the upper range and store in ‘n’
Step 3: If ‘m’ is not less than ‘n’ repeat steps 1 and 2, otherwise go to Step 4
Step 4: Repeat the following steps by assigning values starting from ‘m’ upto ‘n’ on ‘i’
Step 5: Set ‘0’ on ‘index’
Step 6: Assign the value of ‘i’ on ‘j and repeat the following steps until j becomes ‘0’
Step 7: Retrieve a digit from j and check whether it is present in the array ‘arr’
Step 8: If it is present, go to Step 4
Step 9: If not present assign the digit in the array ‘arr’ and increment ‘index’ by ‘1’
Step 10: Eliminate the digit from ‘j’ and go to Step &
Step 11: If ‘j’ becomes ‘0’, display the value of ‘I’ and increment ‘c’ by ‘1’
Step 12: Go to Step 4, if ‘I’ becomes greater than ‘n’ go to Step 13
Step 13: Display ‘c’
No comments:
Post a Comment