-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion_Problem_Set_13.c
More file actions
72 lines (58 loc) · 1.3 KB
/
Recursion_Problem_Set_13.c
File metadata and controls
72 lines (58 loc) · 1.3 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// Sum of first n natural numbers using recursion.
// n :- 1+2+3+4+_ _ _+(n-1)+n = sum(n-1)+n
// sum(n)= sum(n-1)+n
/* #include<stdio.h>
int sum(int n);
int main(){
printf("sum is : %d \n",sum(5));
return 0;
}
// recursive function
int sum(int n){
if(n==1){ // Base case is the condition which stops recursion.
return 1;
}
int sumNm1 = sum(n-1); // sum of 1 to n
int sumN = sumNm1 + n;
return sumN;
} */
// Factorial of n.
//fact(n) = fact(n-1) * n
// n! = (n-1)! * n
/* #include<stdio.h>
int fact(int n);
int main(){
printf("Factorial is : %d \n",fact(5));
return 0;
}
// recursive function
int fact(int n){
if(n==1){ // Base case condition
return 1 ;
}
int factNm1 = fact(n-1); // sum of 1 to n
int factN = factNm1 * n;
return factN;
} */
// Write a function to print n terms of the fibonacci sequence.
// fibonacy(2) = fibonaccy(1) + fibonaccy(0).
// fibonaccy(n) = fibonaccy(n-1) + fibonaccy(n-2).
#include<stdio.h>
int fib(int n);
int main(){
printf("Fibonaccy is : %d", fib(5));
return 0;
}
// recursive function
int fib(int n){
if(n == 0){ // Base case condition
return 0;
}
if (n == 1){
return 1;
}
int fibNm1 = (n-1);
int fibNm2 = (n-2);
int fibN = fibNm1 + fibNm2;
return fibN;
}