File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments