Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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;
- }
- }
- else
- {
- return 0;
- }
- Sum --> 5
- Sum --> 4
- Sum --> 3
- Sum --> 2
- Sum --> 1
- Sum --> 0
- 1 <---
- 2 <---
- 4 <---
- 7 <---
- 11 <---
- 16 <---
- static int Sum(int result, int value)
- {
- if(value == 0)
- return result;
- Sum(result + value, value - 1);
- }
- static int Sum(int value)
- {
- if (value > 0)
- {
- return value + Sum(value - 1);
- }
- else
- {
- return 0; //Change this.
- }
- }
- static unsigned int Sum(unsigned int value) {
- if (value == 0)
- return 0;
- return value + Sum(value - 1);
- }
Add Comment
Please, Sign In to add comment