Anthino Russo - 1 year ago 75
C Question

# Entering integers doesn't stop for non-integers

I have a program in which I enter integers until something that is not an integer is entered. The code needs to print the integers that meet the following condition:

``````integer "abcde": "a > b, b < c, c > d, d < e" or "a < b, b > c, c < d, d > e"
example: 343, 4624, 6231209
``````

I have written this, and it works for most of the integers, but somehow it doesnt work for some.

``````#include <stdio.h>
int main()
{
int a;
while (scanf("%d", &a))
{
int a1 = a;
int cifra = 0, cifra1 = 0, cifra2 = 0;
while (a1 > 0)
{
cifra = a1 % 10;
cifra1 = (a1 / 10) % 10;
cifra2 = (a1 / 100) % 10;
a1 = a1 / 10;

if (cifra == cifra1 || cifra1 == cifra2)
{
break;
}

if ((cifra < cifra1 && cifra1 > cifra2) || (cifra > cifra1 && cifra1 < cifra2)) {

printf("%d\n", a);
break;

}
else
{
break;
}
}
}
return 0;
}
``````

Solved it myself

``````#include <stdio.h>
int main()
{
int a;
int cifra = 0, cifra1 = 0, cifra2 = 0;
int a1;
while(scanf("%d", &a))
{

int a1 = a;
while(a1 > 0){
if(a1 <= 9)
{
break;
}
cifra = a1 % 10;
cifra1 = (a1 / 10) % 10;
cifra2 = (a1 / 100) % 10;

if((cifra > cifra1 || cifra1 > cifra)&&cifra2 == 0)
{
printf("%d\n", a);
break;
}

if((cifra < cifra1&&cifra1 > cifra2) || (cifra > cifra1&&cifra1 < cifra2))
{
a1 = a1 / 10;;

}
else
{

break;
}

}

}

return 0;
}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download