Thread: Counting integer digits

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    207

    Counting integer digits

    I searched for "integer, length" in the forum search & didn't get any matches, so I assume this hasn't been answered:

    How do I count the number of digits in an integer?

    For instance, if the user enters the number one million (1000000) it returns a value of 7 (the number 1 + 6 zeros).

    Do I need to convert it to character data for easier handling or can I do it with an integer?

    Thanks!

    mw

  2. #2
    Registered User
    Join Date
    Nov 2004
    Posts
    11
    Code:
    int num;
    
    if( num < 10 )
    {
       printf( "num is 1 digit" );
    }
    else if( num < 100 )
    {
       printf( "num is 2 digits" );
    }
    
    etc
    Of course what you do with negative numbers is up to you.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    207
    Is there a way to do it by actually counting the digits one at a time? With a dynamic-sized integer?

    My next step will be to convert the number from hexidecimal to decimal, so I'll need to examine each digit individually.

    Thanks!

    mw

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    11
    Code:
    int main( void )
    {
        int *buff;           // stores your data
        int num = 0;      // stores number of digits
        int ch;               // temp store for data
    
        buff = malloc( sizeof( int ) );
    
        while( ( ch = getchar() ) != EOF )
        {
            *buff = ch;
            num++;
            buff = realloc( buff, sizeof( int ) * num + 1 );
            buff += num;   // move to unused space
        }
    
        // add processing code here
    
       free( buff );
    
       return 0;
    }
    something like this maybe( I think it should work!!!), although the digits have to entered individualy. Oh there is no error checking in the above code for brevity.
    Last edited by paul_uk; 05-22-2005 at 03:22 PM.

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    4
    Or better do this
    Code:
    int i;
    int k, num = 1; //
    //initialize  i;etc
    if  (i<0)  i = -i;
    while (i > 10)
    {
        k = i%10;// currently last digit
        i = i/10; //like 1234 becomes 123 etc
        num++; 
    }

  6. #6
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by Letto
    Or better do this
    Code:
    int i;
    int k, num = 1; //
    //initialize  i;etc
    if  (i<0)  i = -i;
    while (i > 10)
    {
        k = i%10;// currently last digit
        i = i/10; //like 1234 becomes 123 etc
        num++; 
    }

    Better still do this

    Code:
    int res = (int) log10(number) + 1;

    Mezzano
    Last edited by Mezzano; 05-22-2005 at 04:07 PM. Reason: forgot me cast

  7. #7
    Registered User
    Join Date
    May 2005
    Posts
    4
    Only that he needs to examine each digit individually

  8. #8
    Registered User
    Join Date
    May 2005
    Posts
    28
    Quote Originally Posted by Letto
    Only that he needs to examine each digit individually
    Yes but with the other ways suggested he must do it twice, he must loop once through the number just to calculate how large the array needs to hold the string representation of it. Then he needs to loop through that array to convert each digit. The way I suggested cuts out the first loop as all he really cares about, according to his original post, is the number of digits in the number. Of course his follow up post doesn't make much sense...he wants to know how to find the number of digits in an integer, fine and good, I gave him that. Then he says he needs to convert from "hex" to decimal. I don't get what he means by this, if he has the value in an integer it is already in decimal form (actually it is in binary and you can interpret that in whatever base you wish), if he has it in a string then he doesn't need to know the length. The whole question is non-sensical.


    Mezzano

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    207
    What I'm trying to do is take a hexidecimal integer of random size & convert it to the decimal equivalent. To do this, I wanted to set up a loop based on the size of the number & multiply each number by 16 to a certain power (depending on the location of the digit).

    For instance, if I have the number 0063FDF0 in hex, I would start from the right digit & move to the left:

    0 * 16 ^ 0 = 0
    F(15) * 16 ^ 1 = 240
    D(13) * 16 ^ 2 = 3328
    F(15) * 16 ^ 3 = 61440
    3 * 16 ^ 4 = 196608
    6 * 16 ^ 5 = 6291456
    0 * 16 ^ 6 = 0
    0 * 16 ^ 7 = 0

    Next I would add these numbers up:
    0 + 240 + 3328 + 61440 + 196608 + 6291456 + 0 + 0 = 6553072

    So 0063FDF0 in hexadecimal is equivalent to 6,553,027 in decimal.

    This probably isn't the best way to do it, but it's the way I was taught.

    mw

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by Lionmane
    What I'm trying to do is take a hexidecimal integer of random size & convert it to the decimal equivalent.
    Do you mean the text representation of an integer value? Can you use [s]printf?
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(void)
    {
       unsigned int hex = 0x63FDF0;
       char *decimal = malloc(log10(hex) + 2);
       if ( decimal )
       {
          sprintf(decimal, "%u", hex);
          printf("hex = %X (%u), decimal = %s\n", hex, hex, decimal);
          free(decimal);
       }
       return 0;
    }
    
    /* my output
    hex = 63FDF0 (6553072), decimal = 6553072
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    sprintf( buf, "%d", number );
    length = strlen( buf );


    Quzah.
    Hope is the first step on the road to disappointment.

  12. #12
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by quzah
    Code:
    sprintf( buf, "%d", number );
    length = strlen( buf );


    Quzah.
    lol. Very nicely done, quzah

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    int length = sprintf( buf, "%d", number );
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    May 2005
    Posts
    207
    So I have to convert the number to text in order to count the digits?

    mw

  15. #15
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    No. But it's the easiest most simplest way.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Assignment HELP!!
    By cprogrammer22 in forum C Programming
    Replies: 35
    Last Post: 01-24-2009, 02:24 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM