Fibonacci Series: It is a special series, where first term is 0 and second term is 1 and from third term onwards, every term is the sum of previous two terms.
0 1 1 2 3 5 8 13 and so on…..
Program (Without Function)
Time Complexity: O(n)
Space Compexity: O(1)
#include <stdio.h> int main() { printf("****************************************************************"); printf("\n****************************************************************"); printf("\n** Program to print Fibonacci Series in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************************"); printf("\n****************************************************************"); int n, i, first = 0, second = 1, third; printf("\nEnter no of terms you want for Fibonacci Series\n"); scanf("%d", &n); printf("\nFibonacci Series is\n"); if (n == 1) printf("%d\t", first); else if (n == 2) printf("%d\t%d\t", first, second); else { printf("%d\t%d\t", first, second); for (i = 3; i <= n; i++) { third = first + second; printf("%d\t", third); first = second; second = third; } } return 0; }
Output 1
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 1 Fibonacci Series is 0
Output 2
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 2 Fibonacci Series is 0 1
Output 3
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 10 Fibonacci Series is 0 1 1 2 3 5 8 13 21 34
Program (With Function)
Time Complexity: O(n)
Space Compexity: O(1)
#include <stdio.h> void fibonacci(int n) { int i, first = 0, second = 1, third; printf("\nFibonacci Series is\n"); if (n == 1) printf("%d\t", first); else if (n == 2) printf("%d\t%d\t", first, second); else { printf("%d\t%d\t", first, second); for (i = 3; i <= n; i++) { third = first + second; printf("%d\t", third); first = second; second = third; } } } int main() { printf("****************************************************************"); printf("\n****************************************************************"); printf("\n** Program to print Fibonacci Series in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************************"); printf("\n****************************************************************"); int n; printf("\nEnter no of terms you want for Fibonacci Series\n"); scanf("%d", &n); fibonacci(n); return 0; }
Output 1
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 1 Fibonacci Series is 0
Output 2
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 2 Fibonacci Series is 0 1
Output 3
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 10 Fibonacci Series is 0 1 1 2 3 5 8 13 21 34
Program (With Recursive Function)
Time Complexity: O(2n)
Space Compexity: O(n)
#include <stdio.h> int fibonacci(int n) { if (n == 1) return 0; else if (n == 2) return 1; else return fibonacci(n - 1) + fibonacci(n - 2); } int main() { printf("****************************************************************"); printf("\n****************************************************************"); printf("\n** Program to print Fibonacci Series in C **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n****************************************************************"); printf("\n****************************************************************"); int n,i; printf("\nEnter no of terms you want for Fibonacci Series\n"); scanf("%d", &n); for(i=1;i<=n;i++) printf("%d\t",fibonacci(i)); return 0; }
Output 1
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 1 Fibonacci Series is 0
Output 2
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 2 Fibonacci Series is 0 1
Output 3
**************************************************************** **************************************************************** ** Program to print Fibonacci Series in C ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** **************************************************************** **************************************************************** Enter no of terms you want for Fibonacci Series 10 Fibonacci Series is 0 1 1 2 3 5 8 13 21 34
Program (With Function) : Method 2 We can take an array to store the terms of fibonacci series generated so far, to reduce time complexity. But it will increase the space complexity, as the memory space will be needed to store the elements of the array.
Time Complexity: O(n)
Space Compexity: O(n)
#include <stdio.h> int fib(int n) { int arr[50]; int i; arr[0] = 0; // First term is zero arr[1] = 1; // Second term is one for (i = 2; i <= n; i++) arr[i] = arr[i - 1] + arr[i - 2]; // Calculating the sum of previous two fibonacci terms for (i = 0; i <= n - 1; i++) printf("%d ", arr[i]); } int main() { int n; printf("***************************************************************"); printf("\n***************************************************************"); printf("\n** WAP to print n terms of a Fibonacci Series **"); printf("\n** Created by Sheetal Garg **"); printf("\n** Assistant Professor **"); printf("\n** Phone No:9467863365 **"); printf("\n***************************************************************"); printf("\n***************************************************************\n"); printf("Enter number of terms to print : "); scanf("%d", &n); printf("The Fibonacci series is : \n"); fib(n); return 0; }
Output 1 :
*************************************************************** *************************************************************** ** WAP to print n terms of a Fibonacci Series ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************** *************************************************************** Enter number of terms to print : 7 The Fibonacci series is : 0 1 1 2 3 5 8
Output 2 :
*************************************************************** *************************************************************** ** WAP to print n terms of a Fibonacci Series ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************** *************************************************************** Enter number of terms to print : 1 The Fibonacci series is : 0
Output 3 :
*************************************************************** *************************************************************** ** WAP to print n terms of a Fibonacci Series ** ** Created by Sheetal Garg ** ** Assistant Professor ** ** Phone No:9467863365 ** *************************************************************** *************************************************************** Enter number of terms to print : 2 The Fibonacci series is : 0 1