Skip to content

Instantly share code, notes, and snippets.

@tjhv
Created June 14, 2019 16:32
Show Gist options
  • Save tjhv/408b60dd6cd3b9847c9cc759ce6f8099 to your computer and use it in GitHub Desktop.
Save tjhv/408b60dd6cd3b9847c9cc759ce6f8099 to your computer and use it in GitHub Desktop.
#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