# My loop doesnt work

• 05-17-2004
Evandb
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

• 05-17-2004
jverkoey
--edit--
post deleted

sorry, i misread what you were trying to do
• 05-17-2004
WaltP
Doesn't anyone teach noobs how to format their code? INDENT! INDENT! INDENT!

Quote:

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?
• 05-17-2004
Evandb
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 :)