Unique Digit Integer Java Program ISC Computer Science 2025 Practical

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 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:

Example 1
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

Example 2
INPUT:
m = 2505
n = 2525
OUTPUT:
THE UNIQUE-DIGIT INTEGERS ARE:
2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2519
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 11

Example 3
INPUT:
m = 2520
n = 2529
OUTPUT:
THE UNIQUE-DIGIT INTEGERS ARE: NIL
FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 0

import java.util.Scanner;
class UniqueDigit{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        System.out.print("m = ");
        int m = Integer.parseInt(in.nextLine());
        System.out.print("n = ");
        int n = Integer.parseInt(in.nextLine());
        if(m > n || m >= 30000 || n >= 30000){
            System.out.println("INVALID RANGE");
            return;
        }
        int count = 0;
        System.out.print("THE UNIQUE-DIGIT INTEGERS ARE: ");
        for(int i = m; i <= n; i++){
            if(isUnique(i)){
                if(count == 0)
                    System.out.print("\n" + i);
                else
                    System.out.print(", " + i);
                count++;
            }
        }
        if(count == 0)
            System.out.print("NIL");
        System.out.println("\nFREQUENCY OF UNIQUE-DIGIT INTEGERS IS: " + count);
    }
    public static boolean isUnique(int n){
        String s = String.valueOf(n);
        for(int i = 0; i < s.length() - 1; i++){
            char ch = s.charAt(i);
            String sub = s.substring(i + 1);
            if(sub.indexOf(ch) >= 0)
                return false;
        }
        return true;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *