-1

I made this coding but I couldn't complete. The idea is should enter numbers and on the screen must shown as separately. Ex) If we enter:1234 result: 1 2 3 4.

#include<stdio.h>
int show_digit(int x);
int main(void)
{
    int x;
    printf("Enter the variable:");
    scanf("%d", &x);
    printf("%d ", show_digit(x));
    return 0;
}
int show_digit(int x)
{
        return show_digit(x / 10)%10;
}
5
  • That's just matter of algorithm!
    – DMaster
    Oct 20, 2015 at 10:04
  • 1
    OK who is downvoting everybody? Oct 20, 2015 at 10:07
  • 1
    @TimBiegeleisen You're the last woman/man standing. :-)
    – cadaniluk
    Oct 20, 2015 at 10:09
  • 2
    @cad I'm a dude. Seems everyone posted too quickly and/or stole their answers from other SO posts. Naughty, naughty! Oct 20, 2015 at 10:10
  • @TimBiegeleisen SO is not Facebook (which has only "like" button)
    – DMaster
    Oct 20, 2015 at 10:12

3 Answers 3

2

You seemed to be trying to make show_digit() recursive, but you got stuck on how to actually do it. This refactored version of your function actually traverses from the last digit of the input to the first (which is the base case), and then starts printing out spaced digits as it comes out of the recursion. Note that I changed the return type of show_digits() to void because it is really now a utility function which does not compute anything.

void show_digit(int x)
{
    if (x < 10)
    {
        printf("%d ", x);
        return;
    }
    else
    {
        show_digit(x/10);
    }

    int the_digit = x % 10;
    printf("%d ", the_digit);

    return;
}
3
  • See your return;, and also see return type of show_digit, it should be void
    – DMaster
    Oct 20, 2015 at 10:14
  • @DMaster Because I changed the signature of the show_digit() method in the OP I feel that maybe it is justified to show how it should be used in the main() method. If you disagree, state your reason and I will remove this extra code. Oct 20, 2015 at 10:18
  • Because I think the answer should be short, it makes ease of read and understand. Also, @AbdulazizMukhamedov is a human, not a machine, he should know how to update his main function without knowing your main function.
    – DMaster
    Oct 20, 2015 at 10:25
0

In this answer, I will not show you any code, but I will explain what's your issue.

I tried to compile your code, it compile successfully, but when I run it, the program crashed, is that your issue?

I know why your program crashed: Out of memory!

How did it happen?

Your function int show_digit(int) is a recursive function, in this function, you didn't made a statement to stop recursion, so, the recursion will continue until the function's stack reach maximum size of allowed memory, then it finally causes crashing.

-1

Non-recursive version:

int digits[256];
int i = 0;
    do {
       digits[i++]=x%10;
       x /= 10;
    } while (x);

for (i=i-1; i>=0; i--) {
   printf ("%d ", digits[i]);
}

It will work fine for positive integers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.