0

How Recursion works in C#

Help I don't understand how " return count += CountDivisions(number);" works in this code please enlighten me. class Program { public static int CountDivisions(double number) { int count = 0; if (number > 0 && number % 2 == 0) { count++; number /= 2; return count += CountDivisions(number); // specially this } return count; } static void Main(string[] args) { double number = 1000; Console.WriteLine("your number is {0}", number); int count = CountDivisions(number); Console.WriteLine(

quot;Total number of evenly divisions: {count}"); // the output is 3 } }

2nd Jul 2019, 11:56 AM
John Andrew
John Andrew - avatar
3 Respostas
+ 2
C# Recursion tutorial https://youtu.be/E7MMHAgxT5Q
2nd Jul 2019, 12:11 PM
Asmit joy
Asmit joy - avatar
+ 1
Thanks! But I don't know how " return count += CountDivisions(number);" works above code.
2nd Jul 2019, 12:25 PM
John Andrew
John Andrew - avatar
+ 1
Okay I got it, basically "return count += CountDivisions(number);" is to run the method again until it exits the condition if the "if statement" become false.
2nd Jul 2019, 12:56 PM
John Andrew
John Andrew - avatar