Counting the number of inegers in a number

This is a discussion on Counting the number of inegers in a number within the C Programming forums, part of the General Programming Boards category; Good morning all, I am trying to created a code that counts the number of integers in a given number. ...

  1. #1
    Registered User
    Join Date
    Sep 2012
    Location
    Tucson, Arizona, United States
    Posts
    13

    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. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    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.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    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;
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User
    Join Date
    Sep 2012
    Location
    Tucson, Arizona, United States
    Posts
    13
    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. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    395
    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. #7
    Registered User
    Join Date
    Sep 2012
    Location
    Tucson, Arizona, United States
    Posts
    13
    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. #8
    Registered User
    Join Date
    Sep 2012
    Location
    Tucson, Arizona, United States
    Posts
    13
    Nevermind guys. My brain is just being slow. I added an if(i>9) and a variable num=ssn for later use. Thanks!

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,308
    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.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 12-04-2011, 10:43 PM
  2. help with a number counting function
    By deathrattle in forum C Programming
    Replies: 3
    Last Post: 03-10-2009, 06:45 AM
  3. counting 16 bit number from a buffer
    By baccardi in forum C Programming
    Replies: 4
    Last Post: 02-20-2009, 03:28 PM
  4. Counting number of frames
    By pradhanparas in forum C# Programming
    Replies: 1
    Last Post: 04-08-2008, 12:11 AM
  5. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 12:25 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21