Digit output in C

Viewed 82

Hello everyone I'm a beginner in coding and I try to figure out how to output all the digits. Example in the number 158 the number 1,5,8,15,58,158. Sorry for the bad English.

I have tried something but it doesnt work for all numbers plus i believe there must be a better way to code it without all the while loops.

    #include <stdio.h>

int main(){
    long num = 5025;
    int num1=num ,num2= num, num3=num;
   
    while(num1 !=0)
    {
        int digit = num1 % 10;
        num1 = num1/10;
        printf("%d\n", digit);
    } 
    while(num2 >10)
    {
        
        int digit = num2 % 100;
        
        num2 = num2 / 10;
                
        printf("%.2d\n", digit);
    }
    while(num3 >100)
    {
        
        int digit = num3 % 1000;
        
        num3 = num3 / 10;
                
        printf("%.3d\n", digit);
    }
    
    return 0;
}
4 Answers

One could print to a string and then post its various combinations

  long num = 158;
  char buf[25];
  snprintf(buf, sizeof buf, "%ld", num);

  for (int first = 0; buf[first]; first++) {
    for (int last = first; buf[last]; last++) {
      int width = last - first + 1;
      printf("%.*s\n", width, buf + first);
    }
  }

Output

1
15
158
5
58
8

Depending on the value, you may get repeats. OP has not yet defined what to do in that case.


For a math only approach, I'd use recursion, yet I expect that is beyond OP's ken at this time.

Here is function printing it for any unsigned int number without fancy printf formats. It also prints in the order from shortest to longest:

void print(unsigned num)
{
    char nums[20];
    size_t len;

    len = sprintf(nums, "%u", num);

    for(size_t seql = 1; seql <= len; seql++)
    {
        for(size_t ndig = 0; ndig < len - seql + 1; ndig ++)
        {
            for(size_t dig = 0; dig < seql; dig++)
            {
                printf("%c", nums[dig + ndig]);
            }
            printf("%c", seql == len ? '\n' : ',');
        }
    }
}

int main(void)
{
    print(15895678);
}

Or without strings:

char getNthDigit(unsigned num, size_t digit, size_t len)
{
    size_t pos = len - digit;
    while(--pos) num /= 10;
    return num % 10;
}

size_t getNdigits(unsigned num)
{
    size_t count = 0;
    while(num) 
    {
        count++;
        num /= 10;
    }
    return count;
}


void print(unsigned num)
{
    char nums[20];
    size_t len;

    len = num ? getNdigits(num) : 1;

    for(size_t seql = 1; seql <= len; seql++)
    {
        for(size_t ndig = 0; ndig <= len - seql; ndig ++)
        {
            for(size_t dig = 0; dig < seql; dig++)
            {
                printf("%c", '0' + getNthDigit(num,dig + ndig, len));
            }
            printf("%c", seql == len ? '\n' : ',');
        }
    }
}

https://godbolt.org/z/Wa8nKns9E

My take, without strings and a bit less complexity. If the number has a 0 digit, it will print duplicates for the digit to the right.

#include <stdio.h>

int main(void)
{
    int digits[64]; // size of array must cover however many digits are in long max
    int numDigits = 0;
    long num = 15876;

    // populate array with each digit
    while (num)
    {
        digits[numDigits++] = num % 10;
        num /= 10;
    }

    // start at the end of the array (since digits are loaded in it backwards)
    for (int first=numDigits-1; first>=0; first--)
    {
        int temp = digits[first];
        printf("%d\n", temp);  // print the initial condition
        for (int last=first-1; last>=0; last--)
        {
            // continue to build temp by multiplying by 10 and adding the next digit
            temp = (temp * 10) + digits[last];
            // could easily put the output in a comma-separated list if that's needed
            printf("%d\n", temp);
        }
    }
}

Output

1
15
158
1587
15876
5
58
587
5876
8
87
876
7
76
6

Demo

#include <stdio.h>

int main(void) {
    int num = -158;
    char text[12];
    sprintf(text, "%d", abs(num));
    
    for(int w=0; w<strlen(text);   ++w)
    for(int s=0; s<strlen(text)-w; ++s)
    printf("%.*s\n", w+1, text+s);
    
    return 0;
}

From IDEOne

Output

Success #stdin #stdout 0s 5632KB
1
5
8
15
58
158
Related