File tree 1 file changed +76
-0
lines changed
1 file changed +76
-0
lines changed Original file line number Diff line number Diff line change
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\n The term in fibonacci sequence at position " );
71
+ cout << n;
72
+ printf (" is " );
73
+ cout << fib (n);
74
+ return 0 ;
75
+
76
+ }
You can’t perform that action at this time.
0 commit comments