Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

package comparatorprogram;

import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Scanner;

class TensPlaceComparator implements Comparator<Integer> {


public int compare(Integer num1, Integer num2) {
int tensPlace1 = (num1 % 100) / 10;
int tensPlace2 = (num2 % 100) / 10;
return Integer.compare(tensPlace1, tensPlace2);
}
}

public class RandomNumbersSort {

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);

System.out.print("Enter the lower bound of the range: ");


int lowerBound = scanner.nextInt();

System.out.print("Enter the upper bound of the range: ");


int upperBound = scanner.nextInt();

System.out.print("Enter the number of random numbers to generate: ");


int count = scanner.nextInt();

List<Integer> randomNumbers = generateRandomNumbers(lowerBound,


upperBound, count);

System.out.println(randomNumbers);

Collections.sort(randomNumbers, new TensPlaceComparator());

System.out.println("\nSorted Numbers According to Tens Place:");


for (int num : randomNumbers) {
System.out.println(num);
}

scanner.close();
}

private static List<Integer> generateRandomNumbers(int lowerBound, int


upperBound, int count) {
List<Integer> randomNumbers = new ArrayList<>();
Random rand = new Random();
int num;

int i=0;
while(i<count) {
num = rand.nextInt(upperBound - lowerBound + 1) + lowerBound;
if (num % 2 == 0 && num % 5 == 0) {
randomNumbers.add(num);
i++;
}
}

return randomNumbers;
}
}

You might also like