Untitled

mail@pastecode.io avatar
unknown
plain_text
21 days ago
1.0 kB
17
Indexable
Never
import java.util.Scanner;

public class SortedArrayChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int[] numbers = new int[5];

        // Prompt the user to enter 5 numbers
        System.out.println("Enter 5 numbers:");

        // Loop to input each number individually and store it in an array
        for (int i = 0; i < 5; i++) {
            System.out.print("Enter number " + (i + 1) + ": ");
            numbers[i] = scanner.nextInt();
        }

        // Check if the array is sorted
        boolean isSorted = true;
        for (int i = 0; i < numbers.length - 1; i++) {
            if (numbers[i] > numbers[i + 1]) {
                isSorted = false;
                break;
            }
        }

        // Report to the user if the array is sorted
        if (isSorted) {
            System.out.println("The array is sorted.");
        } else {
            System.out.println("The array is not sorted.");
        }

        scanner.close();
    }
}