Dremendo Tag Line

Input a number and check digits are in ascending order or not using while loop in C

while Loop - Question 13

In this question, we will see how to input a number and check if the digits are in ascending order or not in C programming using while loop. To know more about while loop click on the while loop lesson.

Q13) Write a program in C to input a number and check if the digits are in ascending order or not using while loop.

Program

#include <stdio.h>
#include <conio.h>

int main()
{
    int n,d,td=0,ld=10,asc=0;

    printf("Enter any number ");
    scanf("%d",&n);

    while(n>0)
    {
        d=n%10;
        if(d<ld)
        {
            ld=d;
            asc=asc+1;	// counting the digits present in the number in ascending order
        }
        td=td+1;		// counting the total number of digits
        n=n/10;
    }

    if(td==asc)
    {
        printf("All digits are in ascending order");
    }
    else
    {
        printf("All digits are not in ascending order");
    }
    return 0;
}

Output

Enter any number 1469
All digits are in ascending order
video-poster