Guest User

Untitled

a guest
Jun 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. static void Main(string[] args)
  2. {
  3.  
  4. Console.WriteLine(Sum(5));
  5. Console.Read();
  6. }
  7.  
  8.  
  9. static int Sum(int value)
  10. {
  11. if (value > 0)
  12. {
  13. return value + Sum(value - 1);
  14. }
  15. else
  16. {
  17. return 1;
  18. }
  19. }
  20.  
  21. else
  22. {
  23. return 0;
  24. }
  25.  
  26. Sum --> 5
  27. Sum --> 4
  28. Sum --> 3
  29. Sum --> 2
  30. Sum --> 1
  31. Sum --> 0
  32. 1 <---
  33. 2 <---
  34. 4 <---
  35. 7 <---
  36. 11 <---
  37. 16 <---
  38.  
  39. static int Sum(int result, int value)
  40. {
  41. if(value == 0)
  42. return result;
  43.  
  44. Sum(result + value, value - 1);
  45. }
  46.  
  47. static int Sum(int value)
  48. {
  49. if (value > 0)
  50. {
  51. return value + Sum(value - 1);
  52. }
  53. else
  54. {
  55. return 0; //Change this.
  56. }
  57. }
  58.  
  59. static unsigned int Sum(unsigned int value) {
  60. if (value == 0)
  61. return 0;
  62. return value + Sum(value - 1);
  63. }
Add Comment
Please, Sign In to add comment