-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.Parameterised and Functional Recursion.cpp
153 lines (111 loc) · 2.62 KB
/
15.Parameterised and Functional Recursion.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#include<bits/stdc++.h>
#include<iostream>
#include <utility>
using namespace std;
// printing the array function
void print(int arr[],int n)
{
for (int i = 0; i < n; i++)
{
/* code */
cout<<arr[i]<<" ";
}
}
// Parameterised and Functional Recursion
// Q.Sum of first N Natural Numbers
// using arithmetic series
void print(int n)
{
int sum = 0;
sum = n*(n+1)/2;
cout<<sum;
}
// using the recursion by parameterised method
void print1(int n , int sum)
{
if (n < 1)
{
cout<<sum;
return;
}
print1(n-1, sum+n);
}
// using recursion by functional method
int print2(int n)
{
if(n == 0) {return 0;}
return n + print2(n-1);
}
// recursive funtion for factorial of n numbers
int fact (int n)
{
if(n == 0){ return 1;}
return n*fact(n-1);
}
// functional recursion
// reverse the array using the recursion
void reverse (int arr[],int l, int r)
{
// two pointers method in recursion
if(l>=r) return;
swap(arr[l],arr[r]);
// recursion call
reverse(arr,l+1,r-1);
}
void reverse1 (int arr[], int i,int n)
{
// base condtion
if(i >= n/2) return;
swap(arr[i],arr[n-i-1]);
reverse1(arr,i+1,n);
}
// function for checking the palindrome or not
// palindrome means reverse of the string will be same
// for exmaple madam if we reverse it m a d a m it again ganerated
// madam same as it actual string before reversing this called palin..
bool palidrome(char string[], int i, int n)
{
if(i >= n/2) return true;
if(string[i] != string[n-i-1])
{
return false;
}
return palidrome(string , i + 1 , n );
}
// fibonacci using the recursion
// we use multiple recursion calls for getting the fibonacci number
int fibonacci(int n)
{
// base condition if the value of n is less then or equal to 1
if(n <= 1)
{
return n;
}
// recursion call
// formula of the fibo f(n) = f(n-1) + f(n-2);
int last = fibonacci(n-1);
int second = fibonacci(n-2);
// returning the sum of last + second element
return last + second;
}
int main (){
// int n;
// cin>>n;
//print1(n,0);
//cout<<print2(n);
// cout<<fact(n);
// reversing the array element
// int arr[] = {1 , 2, 3, 4 , 5};
// int n = sizeof(arr) / sizeof(arr[0]);
// reverse(arr,0,n-1);
// print(arr,n);
// reverse1(arr,0,n);
// print(arr,n);
// char str[] = "skdpnyegmds";
// int n = strlen(str);
// cout<<palidrome(str,0,n);
// fibonacci series nth position using the recursion
int n = 4;
cout<<fibonacci(n);
return 0;
}