KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have just been studying the concept of recursion and I thought that I would try a simple example. In the following code, I am attempting to take the numbers: 1, 2, 3, 4, 5, and add them together using recursion. I expected the result to be 15, but my code is returning 16. What am I doing wrong? Code: static void Main(string[] args) { Console.WriteLine(Sum(5)); Console.Read(); } static int Sum(int value) { if (value > 0) { return value + Sum(value - 1); } else { return 1; } }
Tags (comma-separated)
Save Edits
Cancel