C program for Fibonacci series with recursion and without recursion. In Fibonacci series except first two numbers in series every number is sum of previous two numbers, for example: 2+3 =5, 5+3 = 8. It is very useful in mathematical application as well as in Programming languages.
C Program for Fibonacci_Series by using_Functions | ||
1 | #include<stdio.h> | |
2 | int main() | |
3 | { | |
4 | int n, first = 0, second = 1, next, c; | |
5 | printf("Enter the number of terms\n"); | |
6 | scanf("%d",&n); | |
7 | printf("First %d terms of Fibonacci_series are :-\n",n); | |
8 | for ( c = 0 ; c < n ; c++ ) | |
9 | { | |
10 | if ( c <= 1 ) | |
11 | next = c; | |
12 | else | |
13 | { | |
14 | next = first + second; | |
15 | first = second; | |
16 | second = next; | |
17 | } | |
18 | printf("%d\n",next); | |
19 | } | |
20 | return 0; | |
21 | } |
Output:
C Program for Fibonacci Series By Using Recursion:
C Program for Fibonacci Series by using Recursion | ||
1 | #include<stdio.h> | |
2 | int Fibonacci(int); | |
3 | main() | |
4 | { | |
5 | int n, i = 0, c; | |
6 | scanf("%d",&n); | |
7 | printf("Fibonacci series\n"); | |
8 | for ( c = 1 ; c <= n ; c++ ) | |
9 | { | |
10 | printf("%d\n", Fibonacci(i)); | |
11 | i++; | |
12 | } | |
13 | return 0; | |
14 | } | |
15 | int Fibonacci(int n) | |
16 | { | |
17 | if ( n == 0 ) | |
18 | return 0; | |
19 | else if ( n == 1 ) | |
20 | return 1; | |
21 | else | |
22 | return ( Fibonacci(n-1) + Fibonacci(n-2) ); | |
23 | } |