Dremendo Tag Line

Input 10 numbers and check all numbers are in ascending order or not using while loop in Java

while Loop - Question 10

In this question, we will see how to input 10 numbers and check if all the entered numbers are in ascending order or not in Java programming using while loop. To know more about while loop click on the while loop lesson.

Q10) Write a program in Java to input 10 numbers and check if all the entered numbers are in ascending order or not using while loop.

Program

import java.util.Scanner;

public class Q10
{
    public static void main(String args[])
    {
        int i=1,n,asc=0,lar=0;
        Scanner sc=new Scanner(System.in);

        System.out.println("Enter 10 numbers");
        while(i<=10)
        {
            n=sc.nextInt();
            if(i==1)
            {
                lar=n;			// store the 1st number as largest number
                asc=asc+1;
            }
            else if(n>lar)		// compare the current number with the previous number
            {
                lar=n;
                asc=asc+1;
            }

            i=i+1;
        }

        if(asc==10)
        {
            System.out.println("All entered numbers are in ascending order");
        }
        else
        {
            System.out.println("All entered numbers are not in ascending order");
        }
    }
}

Output

Enter 10 numbers
2
6
9
15
18
20
23
29
56
74
All entered numbers are in ascending order
video-poster