Skip to content

Instantly share code, notes, and snippets.

@ashwanthkumar
Created June 10, 2018 14:07
Show Gist options
  • Save ashwanthkumar/b9dce4ebf19816d71574bc81e4eb83ef to your computer and use it in GitHub Desktop.
Save ashwanthkumar/b9dce4ebf19816d71574bc81e4eb83ef to your computer and use it in GitHub Desktop.
Sum of N numbers without mutating any variables.
#include<stdio.h>
int sum(int n) {
if (n == 1) return 1;
else return n + sum(n - 1);
}
int main() {
int input;
scanf("%d", &input);
const int s = sum(input);
printf("%d\n", s);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment