2

I'm writing a function that is supposed to use recursion to get the number of digits in an integer, e.g. 236 has 3 digits. I have this so far but it's not working. Also, is there a really good explanation for recursion out there?

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
          Console.WriteLine(getDigits(5428, 0));
          Console.ReadLine();
        }

        public static int getDigits(int digits, int i)
        {
            if (digits != 0)
            {
                i++;
                getDigits(digits/10, i);                
            }

            return i;
        }
    }
}
Mat
  • 202,337
  • 40
  • 393
  • 406
David
  • 1,560
  • 2
  • 16
  • 32

3 Answers3

5

Well, you need to tune your recursive function a bit:

public static int GetDigits(int number, int digits)
{
    if (number == 0)
        return digits;

    return GetDigits(number / 10, ++digits);
}

Your mistake is, you're not returning the result from the recursive call.

Thorsten Dittmar
  • 55,956
  • 8
  • 91
  • 139
  • 1
    Sorry, incremented the wrong way. Fixed it. (Explanation: `digits++` increments the value of `digits` *after* the method call. Using `++digits` increments first and calls `GetDigits` recursively afterwords). – Thorsten Dittmar Sep 21 '12 at 11:20
  • There is no reason for the increment operator. Just use `digits + 1`. "Don't make me think." – phant0m Sep 21 '12 at 14:19
0

You are returning i, but when the function goes back from the recursion, the value of i is 1, then you return 1.

You have to return the result of the recursion instead.

return getDigits(...)

public static int getDigits(int digits, int i)
{
    if (digits != 0)
    {
        i++;
        getDigits(digits/10, i);                
    }

    return i;
}

Step #1:

digits = 5428
i      = 0

Step #2:

digits = 542
i      = 1

[...] you increment i, but you are not returning the result of your recursion, you are returning the value of i on step #1.

Now, if you do like this:

public static int GetDigits(int number, int digits)
{
    if (number == 0)
    {
        return digits == 0 ? 1 : digits;
    }

    return GetDigits(number / 10, ++digits);
}

You will be returning the value from the recursion.

Step #1:

number = 5428
digits = 0

Step #2:

number = 542
digits = 1

Step #3:

number = 54
digits = 2

Step #4:

number = 5
digits = 3

Step #5:

number = 0
digits = 4

Step #5 enters the condition of (number == 0) and returns digits, which at this step is 4, to step #4.

Step #4 returns the result of GetDigits(number / 10, ++digits);, which was step #5, to step #3.

Step #3 returns the result of GetDigits(number / 10, ++digits);, which was step #4, to step #2.

Step #2 returns the result of GetDigits(number / 10, ++digits);, which was step #3, to step #1.

Then step #1 returns the value 4 which was the result you got from the last interaction on the recursion.

Explanation on prefix and postfix operations in c# (++digits over digits++)

Community
  • 1
  • 1
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
  • Thanks for your help! This answer is great. Recursion is a bit difficult to get my head around for some reason. I never knew functions could call themselves. I've always just used iteration but am going back through the basics of programming to see what I've missed and what bad habits I've picked up while learning things myself. – David Sep 21 '12 at 21:39
-3

it's wrong condition if (digits != 0) you can't get zero by division and you must use if-statement

if (digits != 0)
{
     i++;
     return getDigits(digits/10, i);                
}
else
{
     return i;
}
burning_LEGION
  • 13,246
  • 8
  • 40
  • 52