Skip to main content

Program in Java: to accept any 10 integers in an array in an Ascending order only . Finally display the sorted array

 Write a program to accept any 10 integers in an array in an Ascending order only . Finally display the sorted array


import java.util.Scanner;

public class AscendingOrderArray {
    public static void main(String[] args) {
        Scanner sc= new Scanner(System.in);
        int[] arr = new int[10];
        
        System.out.println("Enter 10 integers in ascending order:");
        
        for (int i = 0; i < 10; i++) {
            while (true) {
                System.out.print("Enter number " + (i + 1) + ": ");
                int num = sc.nextInt();
                
                // Check if it's the first number or if it's greater than or equal to the previous one
                if (i == 0 || num >= arr[i - 1]) {
                    arr[i] = num;
                    break;
                } else {
                    System.out.println("Please enter a number greater than or equal to " + arr[i - 1]);
                }
            }
        }
        
        // Display the sorted array
        System.out.println("The sorted array is: ");
        for (int i = 0; i < 10; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

Popular posts from this blog