this is based on a problem from an exercise book i found...
in essence you're supposed to write a program that receives an integer (not necessarily positive), and perform the following check on it:
every digit in an odd place should be odd and vise versa.
so for example:
4723 is fine
3301 is not
digits are counted right to left and there's no restriction to the size of the input.
okay so here's what i got:
Code:
#include <stdio.h>
int run=1, input, fixed, i=1, mod, check=1;
int sign_fix(int input);
main()
{
while(run>0)
{
printf("input an integer:\n");
scanf("%d",&input);
fixed=sign_fix(input);
while(fixed>=0,check=1)
{
if((fixed%2)==i) // checks whether %2 is even or odd. i changes after every successful incident
{
i=(i+1)%2;
fixed=fixed/10;
}
else
check=0;
}
if(check>0)
printf("YES \n"); //conforms to the requirements
else
printf("NO \n"); // duh
}
}
int sign_fix(int input) // checks whether input is positive or negative and returns accordingly
{
if(input>=0)
return input;
else
return -input;
}
the code compiles fine, but the program freezes somewhere after the scanf
anyone know what the deal is ?
and, in general, how does one go about solving such problems ?
any tricks ?