Jack Swanson
Jack Swanson

Reputation: 409

Obtaining the Individual Digits from and Integer - C programming

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;
}

Upvotes: 2

Views: 1932

Answers (2)

Stefan Dangubic
Stefan Dangubic

Reputation: 1

Following snippet is pretty straightforward if you need only integers:

while(num != 0)
{
    int digit = num % 10;
    num = num / 10;
    printf("%d\n", digit);
}

Or vice versa:

 while(num > power)
{
  power*=10;
}

power/=10;

while((num != 0) || (power > 0))
{
    int digit = num / power;
    printf("-- %d --\n", digit);
    
    if(digit!=0)
    {
      num= num - (digit * power);
    }
    if(power > 1)
    {
      power/=10;
    }
    else
    {
      power = 0;
    }
}

Upvotes: 0

paxdiablo
paxdiablo

Reputation: 881153

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');
}

Upvotes: 1

Related Questions