Skip to content

Commit 81a28b3

Browse files
added tutorial 18 cpp file from local machine
1 parent 3b4199d commit 81a28b3

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

tut18.cpp

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
// Recursion(Reoccurence)
5+
// it is a method to to call itself directly or indirectly
6+
7+
// Example
8+
// int sum(int num)
9+
// {
10+
// if (num != 0)
11+
// {
12+
// return num + sum(num - 1);
13+
// }
14+
// return 0;
15+
// };
16+
17+
// int factorial(int n)
18+
// {
19+
// if (n > 1)
20+
// {
21+
// return n * factorial(n - 1);
22+
// }
23+
// else
24+
// {
25+
// return 1;
26+
// }
27+
// };
28+
29+
int fib(int n)
30+
{
31+
if (n < 2)
32+
{
33+
return 1;
34+
}
35+
else
36+
{
37+
return fib(n - 2) + fib(n - 1);
38+
}
39+
};
40+
41+
int main()
42+
{
43+
// Factorial of a number
44+
// 6! = 6*5*4*3*2*1 = 720
45+
// 0! = 1
46+
// n! = n*(n-1)!
47+
48+
// Recursion Example 1
49+
50+
// int num;
51+
// cout << "Enter the number : ";
52+
// cin >> num;
53+
// cout << "Sum is : " << sum(num);
54+
55+
56+
// Recursion Example 2
57+
58+
// int n;
59+
// printf("\n\tInput a to find its factorial : ");
60+
// scanf("%d", &n);
61+
// printf("\n\n\tFactorial for the given number is : ");
62+
// cout << factorial(n);
63+
64+
65+
// Recursion Example 3 : Finding n'th term of Fibonacci number
66+
67+
int n;
68+
cout << "Enter the number : ";
69+
cin >> n;
70+
printf("\n\nThe term in fibonacci sequence at position ");
71+
cout << n;
72+
printf(" is ");
73+
cout << fib(n);
74+
return 0;
75+
76+
}

0 commit comments

Comments
 (0)