KnowledgeBoat Logo

Java Number Programs (ISC Classes 11 / 12)

A unique-digit integer is a positive integer (without leading zeros) with no duplicates 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:

Sample Input:
m = 100
n = 120
Sample Output:
The Unique-Digit integers are:
102, 103, 104, 105, 106, 107, 108, 109, 120.
Frequency of unique-digit integers is : 9

Sample Input:
m = 2500
n = 2550
Sample Output:
The Unique-Digit integers are:
2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549.
Frequency of unique-digit integers is: 28.

Java

Java Iterative Stmts

ICSE

32 Likes

Answer

import java.util.Scanner;

public class KboatUniqueIntegers
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter m: ");
        int m = in.nextInt();
        
        if (m < 1 || m > 30000) {
            System.out.println("Value of m should be between 1 and 30000");
            return;
        }
        
        System.out.print("Enter n: ");
        int n = in.nextInt();
        
        if (n < 1 || n > 30000) {
            System.out.println("Value of n should be between 1 and 30000");
            return;
        }
        
        if (m > n) {
            System.out.println("Value of m should be less than n");
            return;
        }
        
        System.out.println("The Unique-Digit integers are:");
        int count = 0;
        for (int i = m; i <= n; i++) {
            int num = i;
            boolean visited[] = new boolean[10];
            boolean isUnique = true;
            
            while (num != 0) {
                int d = num % 10;
                if (visited[d]) {
                    isUnique = false;
                    break;
                }
                visited[d] = true;
                num /= 10;
            }
            
            if (isUnique) {
                count++;
                System.out.print(i + " ");
            }
        }
        System.out.println();
        System.out.println("Frequency of unique-digit integers is: " + count);
    }
}

Output

BlueJ output of A unique-digit integer is a positive integer (without leading zeros) with no duplicates 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: Sample Input: m = 100 n = 120 Sample Output: The Unique-Digit integers are: 102, 103, 104, 105, 106, 107, 108, 109, 120. Frequency of unique-digit integers is : 9 Sample Input: m = 2500 n = 2550 Sample Output: The Unique-Digit integers are: 2501, 2503, 2504, 2506, 2507, 2508, 2509, 2510, 2513, 2514, 2516, 2517, 2518, 2517, 2530, 2519, 2530, 2531, 2534, 2536, 2537, 2538, 2539, 2540, 2541, 2543, 2546, 2547, 2548, 2549. Frequency of unique-digit integers is: 28.

Answered By

8 Likes