forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFibonacciNumber.c
More file actions
43 lines (41 loc) · 754 Bytes
/
FibonacciNumber.c
File metadata and controls
43 lines (41 loc) · 754 Bytes
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
#include <stdio.h>
long long int FibonacciNumber(long long int n)
{
//Because first fibonacci number is 0 and second fibonacci number is 1.
if (n == 1 || n == 2)
{
return (n - 1);
}
else
{
//store last fibonacci number
long long int a = 1;
//store second last fibonacci number
long long int b = 0;
//store current fibonacci number
long long int nth_Fib;
for (long long int i = 3; i <= n; i++)
{
nth_Fib = a + b;
b = a;
a = nth_Fib;
}
return nth_Fib;
}
}
int main()
{
long long int n;
printf("Enter a Number : ");
scanf("%lli", &n);
if (n < 1)
{
printf("Number must be greater than 0");
}
else
{
long long int nth_Fib = FibonacciNumber(n);
printf("Fibonacci Number is %lli", nth_Fib);
}
return 0;
}