1

I am trying to break down a number and obtain each individual digit. I am trying to do that by dividing by powers of ten until reaching 0. Every time I divide by ten it adds to the total of digits in the integer. Once I have the total number of digits I will divide the original digit by 10 to the number of digits - 1 and then decrease the number of digits and repeat the process. This works all the way up until the last digit where I get 4 instead of 6. I have tried using a different number and the last and sometimes second to last digits are the wrong number. Is there any reason as to why this is doing this and any help would be appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main()
{
    int indx;
    int originalDigit = 1234;
    int numDigits;
    int digit;
    int individualDigit;

    numDigits = 0;

    digit = originalDigit;

    while(digit > 0)
    {
        digit = digit / 10;
        numDigits++;

    }

    printf("%d\n", numDigits);

    digit = originalDigit;
    while( digit > 0)
    {
        individualDigit = digit / (int)pow(10, numDigits - 1);

        printf("%d ", individualDigit);
        digit = digit % (int)pow(10, numDigits - 1);
        numDigits--;
    }

    return 0;
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
Jack Swanson
  • 399
  • 1
  • 4
  • 9
  • 1
    Alternative: `sprintf` into a sufficiently sized `char` buffer. You get (a) the digits as chars, and (b) the number of digits via `strlen()`. – WhozCraig Aug 02 '15 at 04:44
  • I believe I need to specify how many variables I need to print out. The assignment I am trying to do is find values from 99999 to 0 of where the sum of each factorial of the individual digits is equal to the number. My approach is to break down the number and use a factorial function and add up the sum. I must continually do this in a loop I assume and I do not know how I would be able to do that with sprintf. – Jack Swanson Aug 02 '15 at 05:08
  • I can not open the link, Sourav. – Jack Swanson Aug 02 '15 at 05:10
  • @RichardFlores I don't follow ,you want this number `1234` to be broken in `1` `2` `3` `4` ? – ameyCU Aug 02 '15 at 05:11
  • (http://ideone.com/5cvqX7) try this. – Sourav Kanta Aug 02 '15 at 05:12
  • Yes thats what I want to do. – Jack Swanson Aug 02 '15 at 05:23
  • Whenever I run that code I get the output; 5 1 2 3 5 3 Is there any reason why we would we get two different outputs? – Jack Swanson Aug 02 '15 at 05:23

1 Answers1

1

I wouldn't use floating point functions such as pow when there are perfectly good ways to do it with integral math.

One solution is to simply sprintf the number into a buffer, then you can use strlen to get the digit count and the individual characters to get the digits:

void method1 (int val) {
    char buff[50];
    sprintf (buff, "%d", val);
    printf ("Digit count = %d, digits =", strlen (buff));
    for (int i = 0; i < strlen (buff); i++)
        printf (" %c", buff[i]);
    putchar ('\n');
}

Another is to load the digits into an array by using your modulo-10 method then reverse the order (either by physically swapping or simply process the array backwards):

void method2 (int val) {
    int len, buff[50];
    for (len = 0; val > 0; len++, val /= 10)
        buff[len] = val % 10;

    // Logical swap.

    printf ("Digit count = %d, digits =", len);
    for (int i = len - 1; i >= 0; i--)
        printf (" %d", buff[i]);
    putchar ('\n');
}

void method3 (int val) {
    int len, buff[50];
    for (len = 0; val > 0; len++, val /= 10)
        buff[len] = val % 10;

    // Pyhsical swap.

    for (int i = 0, j = len - 1; i < j; i++, j--) {
        int t = buff[i];
        buff[i] = buff[j];
        buff[j] = t;
    }

    printf ("Digit count = %d, digits =", len);
    for (int i = 0; i < len; i++)
        printf (" %d", buff[i]);
    putchar ('\n');
}
paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953