Skip to content

Commit 5edfbc0

Browse files
Add files via upload
1 parent 77ad0fa commit 5edfbc0

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

Recursion.c

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Sum of natural numbers using recursion
2+
3+
#include <stdio.h>
4+
int sum(int n);
5+
6+
int main() {
7+
int number, result;
8+
9+
printf("Enter a positive integer: ");
10+
scanf("%d", & number);
11+
12+
result = sum(number);
13+
14+
printf("sum = %d", result);
15+
return 0;
16+
}
17+
18+
int sum(int num) {
19+
if (num != 0)
20+
return num + sum(num - 1); // sum() function calls itself
21+
else
22+
return num;
23+
}

0 commit comments

Comments
 (0)