Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.


import java.util.*;

 

public class Arrays {

   public static void main(String args[]) {

      Scanner sc = new Scanner(System.in);

      int size = sc.nextInt();

      int numbers[] = new int[size];

 

      //input

      for(int i=0; i

          numbers[i] = sc.nextInt();

      }

 

      boolean isAscending = true;

 

   for(int i=0; i

           if(numbers[i] > numbers[i+1]) { // This is the condition for descending order

               isAscending = false;

           }

       }

 

       if(isAscending) {

           System.out.println("The array is sorted in ascending order");

       } else {

           System.out.println("The array is not sorted in ascending order");

       }

      

   }

}



Share to whatsapp

More Questions from Java Basic Codes Module 0

Take an array of numbers as input and check if it is an array sorted in ascending order.

Eg : { 1, 2, 4, 7 } is sorted in ascending order.

       {3, 4, 6, 2} is not sorted in ascending order.


View

Write a function to calculate the factorial of a number.


View

Take an array of names as input from the user and print them on the screen.


View

Searching for an element x in a matrix.


View

Reverse a String (using StringBuilder class) in java.


View

Write a program to print Fibonacci series of n terms where n is input by user :

0 1 1 2 3 5 8 13 21 ..... 

In the Fibonacci series, a number is the sum of the previous 2 numbers that came before it.


View

Write a function which takes in 2 numbers and returns the greater of those two.


View