ISC practical papers › ISC 2007 › Question 3
Unique-digits integers in a range
ISC 2007 Computer Science Practical, Question 3 · Number programs
The question
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, write a program to determine how many unique-digit integers are there in the range between m and n (both inclusive) and output them.
The input contains two positive integers m and n. Assume m < 30000 and n < 30000. You are to output the number of unique-digit integers in the specified range along with their values in the format specified below:
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 IN INTEGERS IS : 9
INPUT: m = 2500
n = 2550
OUTPUT: THE UNIQUE-DIGIT INTEGERS ARE:-
2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549.
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS : 28
Question: ISC 2007 Computer Science Paper 2 (Practical), Question 3, set by the Council for the Indian School Certificate Examinations (CISCE). Solution and video: Vinay Singh.
Java solution
import java.util.*;
class Unique{
private int m,n,frequency;
private String output;
public Unique(int m, int n){
this.m=m;
this.n=n;
findUniqueDigitNumbers();
}
public void findUniqueDigitNumbers(){
frequency=0;
output="THE UNIQUE-DIGIT INTEGERS ARE:-\n\t";
for( int i=m; i<=n; i++){
if(isUniqeNumber(i)){
frequency++;
output+=i+", ";
}
}
output=output.substring(0, output.length()-2)+".";
}
private boolean isUniqeNumber(int n){
boolean unique[] = new boolean[10];
int digit;
for( int temp=n;temp>0; temp/=10){
digit=temp%10;
if( unique[digit]) return false;
unique[digit]=true;
}
return true;
}
public String toString(){
return output;
}
public int getFrequency(){
return frequency;
}
}
class ISC2007Q3 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("INPUT:\tm = ");
int m = in.nextInt();
System.out.print("\tn = ");
int n = in.nextInt();
Unique obj = new Unique(m,n);
System.out.println("OUTPUT:"+obj);
System.out.println("FREQUENCY OF UNIQUE-DIGIT IN INTEGERS IS : "+obj.getFrequency());
in.close();
}
}
All these solutions in one free book
Pragmatic Solutions to ISC Computer Science Practical Problems collects every program from 2004 to 2021 in a 202-page PDF.