# Thread: Counting the number of inegers in a number

1. ## Counting the number of inegers in a number

Good morning all,
I am trying to created a code that counts the number of integers in a given number. For example, 12345= 5. I am stuck trying to get the counter to work properly. The code has more to it in the long run, but I don't want to bother with the rest til this counter is corrected.

I am currently getting an infinite loop, although it is giving me the answer I want. I cannot use anything other than <stdio.h> and basic loops.

Here is what I have so far:

Code:
```#include<stdio.h>
int main(void)
{
long int num, ssn=1;
int i, lim=10;

printf("Enter 9 digit SSN: ");
scanf("%d",&ssn);

for(i=1; ssn>0; i=i+1)
{
ssn=ssn%10;
}

printf("You have entered %d digits. A SSN has 9 digits\n", i);

return(0);
}```
Any suggestions would be greatly appreciated.

2. A number modulus 10 results in a value from 0 to 9. That 0 - 9 value modulus 10 is still only going to ever be 0 - 9. The only way your loop will exit is if the original number has a 0 at the end in which case your logic will only ever return a 1 for the "number of digits". You need to be dividing by 10, not performing modulus.

3. You mean couting the digits, not the "integers".

I am currently getting an infinite loop, although it is giving me the answer
I don't see how it can be giving you the correct answer since you're using the modulus operator when you mean to use the division operator:
ssn /= 10;

4. This is rather embarrassing. I had originally used /10, but the fact that it was still not working was making me try everything. I cleaned my solution and opened a new one with the same code and its working fine. Apparently a previous code was interfering with this one.

hk_mp5kpdw, thanks for the insight anyways. That actually cleared up some confusion with the modulus.

5. Using number % 10 will give you the value of the most right hand digit (the one's digit). Which is sometimes what you want of course, but in your case, you don't need it. As our dedicated Dexter fan mentioned above, just divide by 10.

It's too easy! (A big favorite expression of my high school Geometry teacher). After all these decades, I remember him saying that like it was yesterday. RIP Mr. "Mad dog" Cameron.

6. For what you try to do, you could simply use log10(), or read in a string and use strlen().
About your loop the problem is that when your value becomes less than 10, your operation ssn = sun % 10 always returns the same value, hence your infinite loop.
Just add printf() statements to see it.

7. My counter is replying with the correct number of digits. My problem now is twofold. If the number of digits is between 1-8, I get what I want. Once I have exactly 9 digits, I want to return back the number that the person originally entered. I cannot do this since the for loop brings that number to 0. I am not sure how to store their number separately for use later.

Secondly, if a number with more than 9 digits is entered, it replies with 0. Shouldn't the loop still work with larger numbers?

Code:
```#include<stdio.h>

int main(void)
{
long int  ssn=1;
int i;

printf("Enter 9 digit SSN: ");
scanf("%d",&ssn);

for(i=0; ssn>0; i=i+1)
{
ssn=ssn/10;
}

if(i<9)

printf("You have entered %d digits. A SSN has 9 digits\n", i);
else if(i==9)
printf("The SSN you entered is: %d\n", ssn);

return(0);
}```

8. Nevermind guys. My brain is just being slow. I added an if(i>9) and a variable num=ssn for later use. Thanks!

9. Counting digits is the classic place where you want to use a do..while loop. The reason being that the value 0 is still a one-digit-long string.