Reputation: 1575
I need to determine whether an entered number has digits in ascending order from right to left.
My code seems not working correctly
Here is my code:
int n, temp;
cout << "Please enter number: ";
cin >> n;
bool ascending = true;
temp = n%10;
while (n>0)
{
n /= 10;
if (temp < n % 10)
{
ascending = false;
}
}
if (ascending)
{
cout << "Number is ascending";
}
else {
cout << "Number is not ascending";
}
Upvotes: 3
Views: 5083
Reputation: 638
When I run the latest from Thirupathi, it does work. Note OP said ascending order RIGHT to LEFT.
Ex output runs:
./order
Please enter number: 5321
Number is ascending
./order
Please enter number: 2356
Number is not ascending
Upvotes: 1
Reputation: 2465
You're not updating the value of temp
after every iteration
int n, temp;
cout << "Please enter number: ";
cin >> n;
bool ascending = true;
temp = n%10;
while (n / 10 > 0)
{
n /= 10;
if (temp > n % 10)
{
ascending = false;
break;
}
temp = n % 10;
}
if (ascending)
{
cout << "Number is ascending";
}
else {
cout << "Number is not ascending";
}
Upvotes: 1