Computer Science
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.
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
Related Questions
A Composite Magic number is a positive integer which is composite as well as a magic number.
Composite number: A composite number is a number which has more than two factors.
For example:
Factors of 10 are: 1, 2, 5, 10Magic number: A Magic number is a number in which the eventual sum of the digit is equal to 1.
For example: 28 = 2+8=10= 1+0=1Accept two positive integers 'm' and 'n', where m is less than n. Display the number of composite magic integers that are in the range between m and n (both inclusive) and output them along with frequency, in the format specified below:
Sample Input:
m=10 n=100
Output: The composite magic numbers are 10,28,46,55,64,82,91,100
Frequency of composite magic numbers: 8Sample Input:
m=120 n=90
Output: Invalid inputGiven two positive numbers M and N, such that M is between 100 and 10000 and N is less than 100. Find the smallest integer that is greater than M and whose digits add up to N. For example, if M = 100 and N = 11, then the smallest integer greater than 100 whose digits add up to 11 is 119.
Write a program to accept the numbers M and N from the user and print the smallest required number whose sum of the digits is equal to N. Also, print the total number of digits present in the required number. The program should check for the validity of the inputs and display an appropriate message for an invalid input. Test your program with the sample data and some random data:
Example 1 Example 2 Input:
M = 100
N = 11Input:
M = 1500
N = 25Output:
The required number = 119
Total number of digits = 3Output:
The required number = 1699
Total number of digits = 4Example 3 Example 4 Input:
M = 99
N = 11Input:
M = 112
N = 130Output:
Invalid InputOutput:
Invalid InputA Smith number is a composite number, whose sum of the digits is equal to the sum of its prime factors. For example:
4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.Write a program in Java to enter a number and check whether it is a Smith number or not.
Sample Input: 666
Sum of the digits: 6 + 6 + 6 = 18
Prime factors are: 2, 3, 3, 37
Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
Thus, 666 is a Smith Number.A triangular number is formed by the addition of consecutive integers starting with 1. For example,
1 + 2 = 3
1 + 2 + 3 = 6
1 + 2 + 3 + 4 = 10
1 + 2 + 3 + 4 + 5 = 15
Thus, 3, 6, 10, 15, are triangular numbers.
Write a program in Java to display all the triangular numbers from 3 to n, taking the value of n as an input.