Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.29 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int sum0(int n)
  4. {
  5. return n > 0 ? sum0(n - 1) + n : 0;
  6. }
  7.  
  8. int sum1(int n)
  9. {
  10. return n > 0 ? sum1(n / 10) + (n % 10) : 0;
  11. }
  12.  
  13. int main ()
  14. {
  15. printf("sum0 = %d\n", sum0(1234)); // 0 + 1 + 2 + 3 + 4 + 5...n
  16. printf("sum1 = %d\n", sum1(1234)); // 0 + 1 + 2 + 3 + 4
  17. return 0;
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement