-
Notifications
You must be signed in to change notification settings - Fork 0
/
tut18.cpp
76 lines (62 loc) · 1.32 KB
/
tut18.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
#include <iostream>
using namespace std;
// Recursion(Reoccurence)
// it is a method to to call itself directly or indirectly
// Example
// int sum(int num)
// {
// if (num != 0)
// {
// return num + sum(num - 1);
// }
// return 0;
// };
// int factorial(int n)
// {
// if (n > 1)
// {
// return n * factorial(n - 1);
// }
// else
// {
// return 1;
// }
// };
int fib(int n)
{
if (n < 2)
{
return 1;
}
else
{
return fib(n - 2) + fib(n - 1);
}
};
int main()
{
// Factorial of a number
// 6! = 6*5*4*3*2*1 = 720
// 0! = 1
// n! = n*(n-1)!
// Recursion Example 1
// int num;
// cout << "Enter the number : ";
// cin >> num;
// cout << "Sum is : " << sum(num);
// Recursion Example 2
// int n;
// printf("\n\tInput a to find its factorial : ");
// scanf("%d", &n);
// printf("\n\n\tFactorial for the given number is : ");
// cout << factorial(n);
// Recursion Example 3 : Finding n'th term of Fibonacci number
int n;
cout << "Enter the number : ";
cin >> n;
printf("\n\nThe term in fibonacci sequence at position ");
cout << n;
printf(" is ");
cout << fib(n);
return 0;
}