-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path8_Recursion.c
More file actions
39 lines (30 loc) · 729 Bytes
/
8_Recursion.c
File metadata and controls
39 lines (30 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
#include <stdio.h>
/*Properties of recurison
a.Anything that can be done with iteration,can be done with recursion and vice-versa
b.Recursion can sometimes give the most simple solution
c.Base Case is the condition which steps recursion
d.iteration has infinite loop and recursion has stack overflow*/
void printHW(int count);
int sum(int n);
int main() {
// printHW(5);
int s=sum(3);
printf("Sum is: %d",s);
}
//recursive function
void printHW(int count){
if(count==0){
return;
}
printf("Hello World\n");
printHW(count-1);
}
//sum of n natural numbers
int sum(int n){
if(n==1){
return 1;
}
int sumNm1 = sum(n-1);//sum of 1 to n
int sumN = sumNm1+n;
return sumN;
}