Skip to content

Commit d4b5b4e

Browse files
authored
Merge pull request #700 from The-Keshav-Agarwal/FibonacciNumber
Added Code For nth Fibonacci Number in c
2 parents 36694f7 + b24ae79 commit d4b5b4e

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

fibonacci_number/FibonacciNumber.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <stdio.h>
2+
3+
long long int FibonacciNumber(long long int n)
4+
{
5+
//Because first fibonacci number is 0 and second fibonacci number is 1.
6+
if (n == 1 || n == 2)
7+
{
8+
return (n - 1);
9+
}
10+
else
11+
{
12+
//store last fibonacci number
13+
long long int a = 1;
14+
//store second last fibonacci number
15+
long long int b = 0;
16+
//store current fibonacci number
17+
long long int nth_Fib;
18+
for (long long int i = 3; i <= n; i++)
19+
{
20+
nth_Fib = a + b;
21+
b = a;
22+
a = nth_Fib;
23+
}
24+
return nth_Fib;
25+
}
26+
}
27+
28+
int main()
29+
{
30+
long long int n;
31+
printf("Enter a Number : ");
32+
scanf("%lli", &n);
33+
if (n < 1)
34+
{
35+
printf("Number must be greater than 0");
36+
}
37+
else
38+
{
39+
long long int nth_Fib = FibonacciNumber(n);
40+
printf("Fibonacci Number is %lli", nth_Fib);
41+
}
42+
return 0;
43+
}

0 commit comments

Comments
 (0)