Created
June 14, 2019 16:32
-
-
Save tjhv/408b60dd6cd3b9847c9cc759ce6f8099 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int sum0(int n) | |
{ | |
return n > 0 ? sum0(n - 1) + n : 0; | |
} | |
int sum1(int n) | |
{ | |
return n > 0 ? sum1(n / 10) + (n % 10) : 0; | |
} | |
int main () | |
{ | |
printf("sum0 = %d\n", sum0(1234)); // 0 + 1 + 2 + 3 + 4 + 5...n | |
printf("sum1 = %d\n", sum1(1234)); // 0 + 1 + 2 + 3 + 4 | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment