Program for Fibonacci number in C - Codzify.com

Article by: Manish Methani

Last Updated: September 28, 2021 at 2:04pm IST
1 min 36 sec

Program for Fibonacci number in C
The Fibonacci numbers are the numbers in the following integer sequence.
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

There are various methods to execute the Fibonacci number program using recursion, using dynamic programming, using space optimised method etc.

In this tutorial, we are going to cover two of the most common methods to run the Fibonacci series.

  1. Using Recursion
  2. Using Space optimized method
#include 
void fib(int n, int a, int b)
{ 
  int result;
  result = a + b; 
  printf(",%d", result);
 a = b;  
  b = result; 
  
if(n > 0)
{
 fib(n - 1, a, b); 
}
}

 int main() 
 {
      int a = 0, b = 1;
      printf("0,1");
     fib(8 , a,b);
      return 0;
}

Output

0,1,1,2,3,5,8,13,21,34,55

2) Using Space Optimised method

  
#include
int fib(int n)
{
  int a = 0, b = 1, c, i;
  if( n == 0)
  {
    return a;
 }
  for (i = 2; i <= n; i++)
  {
     c = a + b;
     a = b;
     b = c;
  printf("%d, ",a);
  }
  return b;
}
 
int main ()
{
  int n = 8;
    printf("0, ");
    fib(n);
  return 0;
}

Output

0, 1, 1, 2, 3, 5, 8, 13,

Simplified Coding for Beginners.








Download the Codzify
Mobile App


Mobile App Development, Web App Development, Programming Languages, Latest Tech News & lot more.

Codzify Mobile App

A self-paced learning Courses Created by an Engineer
For Engineers.

Premium

The Complete Angular Course

Instructor: Manish Methani

Explore Curriculum
Free

C Programming for Absolute Beginners

Instructor: Manish Methani

Start Watching
Premium

Flutter Mobile App Development Course

Instructor: Manish Methani

Explore Curriculum
Free

Learn HTML, CSS & Bootstrap

Instructor: Manish Methani

Start Watching

Test your skills with these expert-led curated
Mock Tests.

C Programming Test

Test your C Programming skills with this comprehensive mock test on C Programming.

Take Test

Flutter Test

Solve most asked Interview Questions on Flutter and Test your foundational skills in flutter.

Take Test

GATE(CSE) Operating Systems

Solve most asked GATE Questions in Operating Systems and test your Gate Score.

Take Test

HTML,CSS Test

This is a mock test designed to help you assess your knowledge and skills in HTML and CSS.

Take Test

(GATE CSE) Data Structures & Algorithms Test

Solve most asked GATE Questions in Data Structures and Algorithms and test your Gate Score.

Take Test
include_once 'codzify-footer.php'; ?>