Counting number of digits?

This is a discussion on Counting number of digits? within the C Programming forums, part of the General Programming Boards category; Code: int dat; printf( "\nEnter an int: " ); scanf( "%d", &dat ); printf( "\nThe counts of digits:\n0 %d\n" "1 ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Counting number of digits?

    Code:
    int dat;
    
      printf( "\nEnter an int: " );
      scanf( "%d", &dat );
      
      printf( "\nThe counts of digits:\n0  %d\n"
              "1  %d\n"
              "2  %d\n"
              "3  %d\n"
              "4  %d\n"
              "5  %d\n"
              "6  %d\n"
              "7  %d\n"
              "8  %d\n"
              "9  %d\n", );
    I'm trying to get the user to enter an integer and then make the output print the number of digits entered. Ex. If I enter 1123 or some other number, how do I make it display this? ( and for any number I enter )
    0 0
    1 2
    2 1
    3 1
    4 0
    5 0
    6 0
    7 0
    8 0
    9 0

    The right column shows the numbers I entered and how many times it occured. I'm not sure know how to extract the digit using loops, "if", etc and compare it with the # to make it show the number of times it occured.
    Last edited by scatterice; 05-11-2009 at 11:25 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,937
    For starters, how would you isolate the individual digits of the integer entered by the user?

    Another thing to think of: must you actually store the input as an int, or can you just store it as a string?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I think I would enter dat % 10 to get the 1st digit. (dat * .1) % 10 to get the 2nd digit and so on. Then do I use "if" to see what digit it is? After I get the digit, how would I put it back in the printf function?

  4. #4
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Quote Originally Posted by scatterice View Post
    I think I would enter dat % 10 to get the 1st digit. (dat * .1) % 10 to get the 2nd digit and so on.
    Remember that dat * .1 is the same as dat/10, which is easier to read and satisfies the constraints of integer division. Now, get a new variable (or just stick with the old one), assign it the value of dat/10 and repeat the whole thing until dat/10 < 10.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    I see a printf() call containing 10 %d conversions and NO parameters.
    What are you expecting here?
    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.

  6. #6
    Registered User
    Join Date
    Apr 2009
    Location
    Russia
    Posts
    116
    it limited for int (when you are enter a big value it will transform to anything)

    output:

    Code:
    [guest@station eng]$ ./test
    Enter a number: 1237034079
    0 - 2
    1 - 1
    2 - 1
    3 - 2
    4 - 1
    5 - 0
    6 - 0
    7 - 2
    8 - 0
    9 - 1
    [guest@station eng]$
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    May 2009
    Location
    Slovenia
    Posts
    5
    Hello.

    As laserlight already suggested, I would solve this problem by using input as a string and not as int. This method has several advantages over the integer method:
    1. it's not limited by the maximum number the int can hold
    2. it's easier to code
    3. it teaches you about character encoding, which is something really useful

    AS you probably know, character are (like everything else) represented by numbers. Mapping of characters to numbers is called encoding and it enables us to convert numbers to something we're able to read.

    There are many encodings used in computing, but most prevalent are ASCII, ISO-8859-1 and UTF-8. And really important thing to know is that all of them map first 128 character exactly the same way. You can see ASCII table here.

    Now to solve the problem. Steps that are involved, could be described as:
    1. obtain input from user
    2. parse and validate input
    3. return results
    Obtaining input from user should be done like this, since we need a string:
    Code:
    scanf( "%s", string );
    Now that we have data to work on, we can examine characters in the string on by one and validate the input. If you looked at the ASCII charmap I provided link to, you can see that digits are mapped to numbers 48 to 57 and this is what we're after. We can test this using simple if statement:
    Code:
    if( character > 47 && character < 58 )
    or
    Code:
    if( character >= '0' && character <= '9' )
    All that is left now is to count the digits. See full sources for more information about this and post back if you have any questions.
    Attached Files Attached Files

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Just use some basic math in a loop. This is a common homework problem. That's the common solution.


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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number of digits in a variable
    By Saeid87 in forum C Programming
    Replies: 6
    Last Post: 06-05-2009, 01:13 PM
  2. adding a number to a number
    By bigmac(rexdale) in forum C Programming
    Replies: 11
    Last Post: 10-24-2007, 12:56 PM
  3. Counting letters and digits
    By FeNCinGeR in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2006, 11:39 AM
  4. Counting integer digits
    By Lionmane in forum C Programming
    Replies: 22
    Last Post: 05-24-2005, 10:11 AM
  5. Finding digits in a number
    By MadStrum! in forum C Programming
    Replies: 5
    Last Post: 09-05-2002, 03:05 AM

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