15,795,793 members
1.00/5 (1 vote)
See more:
split the given integer into digits and print the number of "5" present in the integer using recursion

What I have tried:

C++
```#include <stdio.h>

int extract (int);

int main()
{
int Number, r = 0;

scanf("%d", &Number);

r = extract(Number);

printf(" %d", r);
return 0;
}

int extract (int Number)
{
int Reminder, c=0;

if(Number > 0)
{
Reminder = Number % 10;
if(Number == 5)
{
c=c+1;
}
else
{
c=0;
}
extract (Number / 10);
return c;
}
else
return 0;
}```

Posted
Updated 23-Jan-17 9:52am
v2
Richard MacCutchan 23-Jan-17 10:50am
Your recursion does not work because you ignore the value returned from it inside your extract function.

## Solution 1

Well...because you set the count to zero every time you meet a digit that isn't '5'.
So if the most significant digit is 1, 2, 3, 4, 6, 7, 8, or 9 you will alway return 0 - regardless of the number of '5' digits in the user input.

Have a look for yourself - use the debugger and it will show you what is going on.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Time for you to learn a new (and very, very useful) skill: debugging!

Member 12963806 23-Jan-17 10:13am
any debugger tools online?
OriginalGriff 23-Jan-17 10:29am
Why not use the one for the system you are compiling on and with?

## Solution 2

I see 3 different reasons why your code don't work.
one of them is:
C++
`extract (Number / 10);`

In the recursive call, you just discard the result, it is an error.
Do yourself a favor and learn how to use the debugger.

When you don't understand what your code is doing or why it does what it does, the answer is debugger.
Use the debugger to see what your code is doing. It allow you to execute lines 1 by 1 and to inspect variables as it execute, it is an incredible learning tool.

Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't find bugs, it just help you to. When the code don't do what is expected, you are close to a bug.