code.cpp 206 B

12345678
  1. /* Write a function that finds the sum of the first n
  2. * natural numbers. Make your function recursive.
  3. * sum(5) ➞ 15
  4. * 1 + 2 + 3 + 4 + 5 = 15
  5. */
  6. int sum(int n) {
  7. return (n>0) ? n+sum(n-1):0 ;
  8. }