Separating a Number into its Digits

This is a discussion on Separating a Number into its Digits within the C Programming forums, part of the General Programming Boards category; How do you separate an integer in its individual digits without using an array? I'm thinking I have to divide ...

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Separating a Number into its Digits

    How do you separate an integer in its individual digits without using an array? I'm thinking I have to divide it somehow...

    I need to take the individual digits from an integer and convert them into their respective english names. I have seen answers with arrays, but I have not yet learned about arrays in the book that I'm using.

  2. #2
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    So, if you have an integer 125, you want to print "one two five"?

    I guess the array solution is to turn it into a string and go through each character.

    Without arrays... indeed, you can get each digit out using division. To print from left to right you'll have to pick an upper limit. Say you decided your limit is 4 digits (it should be higher ), you'd first divide by 10^3, then 10^2, 10^1, 10^0 to get each digit. You can easily do this in a loop. Just don't forget that in C, raising something to a power is done with pow(), not with "^" as you might expect.

    Pseudo code ish:

    Code:
    	<i is the number input>
    
    	index = 6
    	while index >= 0
    		partial = i/(10.0^index)
    		if (partial != 0)
                            print word for partial 
    		i = i - partial * (10.0^index)
    
    		index = index - 1;
    Yuck. Wonder if someone else can do better

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    I thought it might help if I posted the code that I have so far:

    // Program to take integers entered and display each digit in english.


    Code:
    #include <stdio.h>
    
    
    int main (void)
    {
        int integer = 0, output = 0;
    
    
        printf ("Enter an integer: ");
        scanf ("%i", &integer);
    
    
        if ( integer > 99999999 )
            printf ("Integer is too large!");
    
    
        switch (output = integer % 10)
        {
            case 0:
                printf (" Zero ");
                break;
            case 1:
                printf (" One ");
                break;
            case 2:
                printf (" Two ");
                break;
            case 3:
                printf (" Three ");
                break;
            case 4:
                printf (" Four ");
                break;
            case 5:
                printf (" Five ");
                break;
            case 6:
                printf (" Six ");
                break;
            case 7:
                printf (" Seven ");
                break;
            case 8:
                printf (" Eight ");
                break;
            case 9:
                printf (" Nine ");
                break;
        }
    
    
        return 0;
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    There's an easier way to peel off the digits of a number. This works by "peeling" off the 1's digit, with every iteration through the loop. In pseudo code

    Code:
    while(number > 0) 
       variable = the number % 10
       divide the number by 10
       logic to print the variable's word, in here
    end while loop

  5. #5
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by Adak View Post
    There's an easier way to peel off the digits of a number. This works by "peeling" off the 1's digit, with every iteration through the loop. In pseudo code

    Code:
    while(number > 0) 
       variable = the number % 10
       divide the number by 10
       logic to print the variable's word, in here
    end while loop
    This separated the digits but also reversed the so 567 = Seven Six Five

    Inserted it like this:

    Code:
        if ( integer > 99999999 )
            printf ("Integer is too large!");
    
    
        while ( integer > 0 )
        {
            output = integer % 10;
            integer = integer / 10;
                if ( output != 0 );
                    switch ( output )

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1. Each time through the loop divide by that power of 10.
    One you get your first non-zero result then you start outputting text.
    Also, subtract the required amount to remove that digit as you go.
    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"

  7. #7
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by iMalc View Post
    If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1. Each time through the loop divide by that power of 10.
    One you get your first non-zero result then you start outputting text.
    Also, subtract the required amount to remove that digit as you go.
    What would that code look like?

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,192
    Quote Originally Posted by iMalc View Post
    If you want the digits in the correct order then an easy way is to start with the largest power of 10 (i.e. 1 billion) and then work your way down towards 1.
    That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.

    This strikes me as a bit of a homework problem. However, the solution involves using some means of temporary representation for data of varying size, presumably distinct from an array. There is an anomaly in the question in that the words "one", "two", etc are also technically represented as arrays.
    Right 98% of the time, and don't care about the other 3%.

  9. #9
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Quote Originally Posted by grumpy View Post
    That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.

    This strikes me as a bit of a homework problem. However, the solution involves using some means of temporary representation for data of varying size, presumably distinct from an array. There is an anomaly in the question in that the words "one", "two", etc are also technically represented as arrays.
    The book does say that its a difficult exercise. I'm not taking a class, just learning on my own from a book and some free lectures online.

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    6,192
    It's not that difficult.

    An array is just a data structure that supports random access to elements that have a defined order. There are other data structures you can use in order to retrieve elements in reverse order, relative to how they are stored.
    Right 98% of the time, and don't care about the other 3%.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,292
    Quote Originally Posted by grumpy View Post
    That hard codes an upper limit on what values can be handled. It certainly won't work for long long types.
    Yes indeed it means limiting the maximum number, and in fact if this were used on a compiler that had 16-bit ints then it would require a different maximum value. But if one wants to avoid using an array, then short of using recursion instead its probably the best option.
    Portability is naturally less of a concern for a beginner.

    How about using a lookup table for the number names.
    Last edited by iMalc; 12-30-2011 at 09:21 PM.
    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"

  12. #12
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Or you could take the number as a string and walk it. Or you could take the number as some integer type, and use recursion.


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

  13. #13
    Registered User
    Join Date
    Oct 2010
    Posts
    114
    Hi, Peteski, try this code that separates a 5-digit integer into its digits:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        int d1, d2, d3, d4, d5, number;
    
        printf("Enter a five-digit number: ");
        scanf("%d", &number);
    
        d5 = number % 10;
        d4 = (number / 10) % 10;
        d3 = ((number / 10) / 10) % 10;
        d2 = (((number / 10) / 10) / 10) % 10;
        d1 = ((((number / 10) / 10) / 10) / 10) % 10;
    
        printf("The first digit is  %d.\n", d1);
        printf("The second digit is %d.\n", d2);
        printf("The third digit is  %d.\n", d3);
        printf("The fourth digit is %d.\n", d4);
        printf("The fifth digit is  %d.\n", d5);
    
        return 0;
    }
    Last edited by stdq; 12-31-2011 at 10:33 AM.

  14. #14
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    You don't need to store each of the digits in an int, as their range is from zero to nine. A simple char would work.

    Also, replace the calculations with the following, as it does less operations and is more efficient.

    Code:
        d5 = number % 10;
        d4 = (number / 10) % 10;
        d3 = (number / 100) % 10;
        d2 = (number / 1000) % 10;
        d1 = (number / 10000) % 10;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. need help separating a real number
    By jorgejags in forum C Programming
    Replies: 4
    Last Post: 09-16-2008, 10:45 AM
  2. How do I add the digits of a number ?
    By abh!shek in forum C Programming
    Replies: 16
    Last Post: 02-06-2008, 04:57 AM
  3. Number of digits in a decimal number
    By maverix in forum C Programming
    Replies: 7
    Last Post: 11-04-2007, 11:12 AM
  4. Number of digits?
    By falador in forum C Programming
    Replies: 2
    Last Post: 05-13-2004, 03:52 AM
  5. Separating an Integer into digits
    By Mr Curtains in forum C Programming
    Replies: 2
    Last Post: 03-22-2003, 02:58 AM

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