-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecurision.cpp
91 lines (81 loc) · 1.69 KB
/
recurision.cpp
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <cmath>
using namespace std;
int factorial(int n){
if(n==1){
return 1;
}
return factorial(n-1)*n;
}
int fibonacci(int n){
if(n==1){
return 1;
}
if(n==0){
return 0;
}
return fibonacci(n-1)+fibonacci(n-2);
}
int power(int x,int n){
if(n==0){
return 1;
}
return power(x,n-1)*x;
}
int nodigits(int n){
if(n==0){
return 0;
}
return nodigits(n/10)+1;
}
int sumofdigits(int n){
if(n==0){
return 0 ;
}
return sumofdigits(n/10)+n%10;
}
int multiplication(int m,int n){
if(n==0){
return 0 ;
}
return multiplication(m,n-1)+m;
}
int countzero(int n){
if(n==0){
return 0;
}
int count=0;
int answer=countzero(n/10);
int lastdigit=n%10;
if(lastdigit==0){
return answer+1;
}else{
return answer;
}
}
double geometric_sum(int n){
if(n==0){
return 1;
}
return geometric_sum(n-1)+(1.0/pow(2,n));
}
int sumofarray(int a[],int n){
if(n==0){
return 0;
}
else{
return a[0]+sumofarray(a+1,n-1);
}
}
int main() {
int nCount[] = {1, 2, 3, 4, 5};
cout<<"factorial of number is "<<factorial(3)<<endl;
cout<<"fibonacci of number is "<<fibonacci(4)<<endl;
cout<<"power of number is "<<power(4,2)<<endl;
cout<<"no of digits in number is "<<nodigits(4)<<endl;
cout<<"sum of digits in number is "<<sumofdigits(14)<<endl;
cout<<"multiplication of number is "<<multiplication(4,2)<<endl;
cout<<"geometric sum of number is "<<geometric_sum(3)<<endl;
cout<<"sum of array elements are "<<sumofarray(nCount,5);
return 0;
}