# Thread: My loop doesnt work

1. ## My loop doesnt work

Hello,
I am trying to get my program to loop through an integer which will have 1-10 digits and check to make sure they all are 1's and 0's. At the moment, my code doesnt work. It gives me an answer as long as I enter a 1 or 0 as the last digit. I suspect it is a problem with my use of the modulus operator. Here is what I have:

Code:
```while(control<10)
{
printf("Enter a binary number (1-10 digits):");
scanf("%d",&binary);

for(i=0;i<10;i++)
{
temp=binary%10;
if(temp==0||temp==1)
{
control+=1;
}
else
{
control=0;
break;
}
}
}```
Thanks in advance for any and all help.

Evan

2. --edit--
post deleted

sorry, i misread what you were trying to do

3. Doesn't anyone teach noobs how to format their code? INDENT! INDENT! INDENT!

Originally Posted by Evandb
Hello,
I am trying to get my program to loop through an integer which will have 1-10 digits and check to make sure they all are 1's and 0's. At the moment, my code doesnt work. It gives me an answer as long as I enter a 1 or 0 as the last digit. I suspect it is a problem with my use of the modulus operator.
No, the problem is your for loop simply tests the last digit 10 times. After testing the digit, you need to remove it so you can test the next to last digit.

Then, consider what happens when you enter only 5 digits? What happens to your loop then?

4. Yeah, I am an idiot. I forgot to add binary=floor(binary/10); to the loop so it would test the next digit. Thanks for your response