PRINT THE NUMBER IN ASCENDING AND DESCENDING ORDER

In the above programe using the for loop we can get the numbers in ascending and descending order.

The logic in first for loop while incrementing i value we will get ascending order.

The logic in second for loop while decrementing i value we will get descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.util.*;
public class AscenDscendOrder
{
    public static void main(String[]args)
    {

 

        int num[]={10,6,1};
        int l=num.length;
        int i;
        System.out.println("given numbers are:");
        Scanner x = new Scanner(System.in);
        x = in.nextInt();
        for(i=0; i<l;i++)
        {  
            System.out.println(""+num[i]);
            System.out.println("ascending order:");
            Arrays.sort(num);
        }
        for(i=0; i<l;i++)
        {
            System.out.println(" "+num[i]);}
            System.out.println("descending order:");
            Arrays.sort(num);  
            for(i=2;i>=0;i--)
            {
                System.out.println(" "+num[i]);
            }
        }
    }
}

Output :Ascending and Descending Order

oddeven