Program opened below. You can scroll through the algorithm, Java code and output.

Unique-Digit Integers in a Range

Download .java

Question

Accept positive integers m and n with m < n < 30000. List all numbers in the inclusive range whose decimal digits are all distinct and print their frequency.

Step 1: Start. Step 2: Declare the variables and data structures required for the task. Step 3: Start and input m and n. Step 4: Validate 1 <= m < n < 30000. Step 5: Initialize the counters, flags, accumulators or result structures needed by the solution. Step 6: For each value x from m through n, convert x to a string. Step 7: Use a set or a boolean array to check whether any digit repeats. Step 8: If all digits are distinct, append x to the result list. Step 9: Display the list or NIL if empty. Step 10: Display the number of values found. Step 11: Stop.
Java
import java.util.*;
class UniqueDigitRange {
    static boolean unique(int n) {
        boolean[] seen = new boolean[10];
        while (n > 0) {
            int d = n % 10;
            if (seen[d]) {
                return false;
            }
        seen[d] = true;
        n /= 10;
    }
return true;
}
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int m = sc.nextInt(), n = sc.nextInt();
    if (m < 1 || m >= n || n >= 30000) {
        System.out.println("INVALID INPUT");
        return;
    }
ArrayList<Integer> found = new ArrayList<>();
for (int x = m; x <= n; x++) {
    if (unique(x)) {
        found.add(x);
    }
}
System.out.println("THE UNIQUE-DIGIT INTEGERS ARE:");
System.out.println(found.isEmpty() ? "NIL" : found.toString().replace("[", "").replace("]", ""));
System.out.println("FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: " + found.size());
}
}
Input: 100 120 Output: THE UNIQUE-DIGIT INTEGERS ARE: 102, 103, 104, 105, 106, 107, 108, 109, 120 FREQUENCY OF UNIQUE-DIGIT INTEGERS IS: 9
← Previous Program 63 of 84 Next →

Share a comment or request a program

Use this form to send a comment, report an issue, or suggest a new practical. Your message will be saved for review by the school.