How can I check if a number is descending, and have a flag that would indicate yes or no? Not an array.
In the last loop, last or x take value 0, and because of that answer is not correct.
while(n>1)
{
last=n;
x=n/10;
if(last>x){
flag=0;
}
if(last<x){
flag=1;
}
n=n/10;
}
if(flag==0) printf("0\n");
else printf("1\n");
CodePudding user response:
#include <stdio.h>
#include <stdbool.h>
bool isDescending(int n)
{
bool descending=true;
while(n/10 && descending)
{
int d=n;
n/=10;
descending = d<n;
}
return descending;
}
int main(void) {
int test[] = { 123456, 975310 };
for(int i=0; i<2; i)
{
printf("Descending %d: %s\n", test[i], isDescending(test[i])? "Yes" : "No" );
}
return 0;
}
Result:
Success #stdin #stdout 0s 5432KB
Descending 123456: No
Descending 975310: Yes