Dremendo Tag Line

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

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 C++ programming using while loop. To know more about while loop click on the while loop lesson.

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

Program

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int i=1,n,asc=0,lar=0;

    cout<<"Enter 10 numbers\n";
    while(i<=10)
    {
        cin>>n;
        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)
    {
        cout<<"All entered numbers are in ascending order";
    }
    else
    {
        cout<<"All entered numbers are not in ascending order";
    }
    return 0;
}

Output

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