Number of digits in a decimal number

This is a discussion on Number of digits in a decimal number within the C Programming forums, part of the General Programming Boards category; Hi All, Given an integer n of ndigits, I want to write a function which returns a value which is ...

  1. #1
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46

    Number of digits in a decimal number

    Hi All,

    Given an integer n of ndigits, I want to write a function which returns a value which is 1 followed by (ndigits-1) number of zeroes.
    Example: If I give 12345, I should get 10000

    The below code does the same. However, I would like to know if there is a more efficient way(s) to obtain the same result. Kindly post your thoughts regarding this:

    Code:
    int func(int n)
    {
      int i;
      for(i=1; n>10; n/=10,i*=10)
      	;
      	
      return i;
    }
    Regards
    maverix

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    As in say
    if ( n >= 10000 && n < 100000 ) result = 10000;
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Its not with only 5 digit integers, the integer can be any number between 1 and INT_MAX.

    I would like to know if there is a way to reduce the number of iterations in the for loop, I just want to optimize the code so that it takes little time to give out the result.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your efficiency shouldn't be a problem. Your loop will only execute 1 time for each power of 10, so n = 1,000,000, is only 6 loops. That's less than the blink of your eye, even on a slow computer.

    If your code is running slow, I'd certainly look for the cause, in it's other functions, not here.
    Last edited by Adak; 11-04-2007 at 10:02 AM.

  5. #5
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You can combine division and test into one statement
    Code:
    int func(int n) {
      int i;
      for(i=1; n/=10; i*=10);
      return i;
    }
    Kurt

  6. #6
    abyss - deep C
    Join Date
    Oct 2007
    Posts
    46
    Hi All,
    Thanks for all the replies.

    Hi Adak,
    My code is not slow because of this function...and I am sure other parts of the code is also fine.
    However, I just wanted to know if there is some other mathematical way using which I could get the result.

    One way I thought of is mentioned below:
    Ex:
    n = 234567
    chop off the left most significant digit
    subract the number 34567 from 234567
    divide the resulting number 200000 by the left most significant digit 2

    I am finding it difficult to put the above into C code.
    Also, I am not sure if this would improve the efficiency.

    If there are any other ways to implement, please let me know.
    I just wanted to know various methods to implement the same.

    Cheers,
    maverix

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,490
    Take my code and copy/paste it 9 times, for each of the possible powers of 10 which can be represented in 32-bits.

    For even more efficiency, you can reduce this to at MOST 4 comparisons by arranging the cascade of if/else branches in the right way.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  8. #8
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    If you want the mathematical way, you can use logarithms, but Salem's way will be faster.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 05:55 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Learning Memory, Ins and Outs?
    By Zoiked in forum C Programming
    Replies: 1
    Last Post: 08-27-2007, 04:43 PM
  4. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM
  5. Help! Can't read decimal number
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-07-2001, 02:09 AM

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