Skip to main content

Fibonacci series

Fibonacci series are series of numbers that start with 1 and 1, and then each subsequent number is the sum of the previous two. For example, 1, 1, 2, 3, 5, 8, 13, ... are Fibonacci series. These series have many applications and properties in mathematics, nature, art and computer science.


In this blog post, I will show you how to write a C program that prints the Fibonacci series up to a given number. The program will use a recursive function that calls itself with a smaller argument until it reaches the base case of zero or one. Then it will return the Fibonacci number for that argument and print it on the screen.


The source code for the program is:


#include <stdio.h>


int fibo (int n){

    // base case: return 1 if n is zero or one

    if (n == 0 || n == 1){

        return 1;

    }

    // recursive case: return the sum of fibo(n-1) and fibo(n-2)

    else{

        int result = fibo(n-1) + fibo(n-2);

        printf("%d ", result); // print the result

        return result;

    }

}


int main ( ) {

    // call the fibo function with 23 as the argument

    fibo(23);

    printf("\n"); // print a new line

    return 0;

}

I hope you enjoyed this blog post and learned something new about Fibonacci series and recursion. If you have any questions or feedback, please leave a comment below. Thank you for reading!

Comments