-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactorial-fibbonaci.c
61 lines (54 loc) · 1.04 KB
/
factorial-fibbonaci.c
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
/*
* Fibbonaci Concept & Source: https://www.tutorialspoint.com/data_structures_algorithms/fibonacci_series.htm
*/
#include <stdio.h>
#include <stdlib.h>
int factorial(int n)
{
if (n == 0)
return 1;
return n * factorial(n - 1);
}
int fibbonaci(int n)
{
if (n == 0)
return 0;
else if (n == 1)
return 1;
else
{
return (fibbonaci(n - 1) + fibbonaci(n - 2));
}
}
int main()
{
int n = 8;
printf("Factorial of %d is %d \n", n, factorial(n));
int n1 = 0, n2 = 1, temp;
//recursive method
printf("\n RECURSIVE METHOD \n");
for (int i = 0; i < n; i++)
{
printf("%d ", fibbonaci(i));
}
//non recursive method
printf("\n NON RECURSIVE METHOD \n");
for (int i = 0; i < n; i++)
{
if (i == 0)
{
printf("0 ");
}
else if (i == 1)
{
printf("1 ");
}
else
{
printf("%d ", n1 + n2);
n2 = n1 + n2;
n1 = n2 - n1;
}
}
return 0;
}