C Programs to Print Fibonacci Sequence

The Fibonacci Sequence is a series of numbers in which each number (Fibonacci number) is the sum of the two preceding ones. The sequence often starts with 0 and 1. In mathematical terms, the sequence is defined by the recurrence relation:

Fn = Fn-1 + Fn-2

with seed values: F0 = 0 and F1 = 1.

The first few Fibonacci numbers are: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on.

In this article, we will see how to generate Fibonacci numbers in the C programming language using two different methods. The first method will print the first ‘n’ Fibonacci numbers, and the second one will print all Fibonacci numbers up to a given maximum number.

C Program to Print First ‘n’ Fibonacci Numbers

Let’s write a C program that prints first N numbers of Fibonacci sequence.

#include 

void printFibonacciNumbers(int n) {
    int f1 = 0, f2 = 1, i;

    if (n < 1)
        return;
    printf("%d ", f1);
    for (i = 1; i < n; i++) {
        printf("%d ", f2);
        int next = f1 + f2;
        f1 = f2;
        f2 = next;
    }
}

int main() {
    int n;
    printf("Enter the total number of Fibonacci numbers to print: ");
    scanf("%d", &n);
    printFibonacciNumbers(n);
    return 0;
}

In the above code, the function printFibonacciNumbers(int n) takes an integer ‘n’ as input and prints the first ‘n’ numbers of the Fibonacci series. The loop in this function runs ‘n’ times and calculates the next Fibonacci number by adding the last two Fibonacci numbers. It then updates the last two Fibonacci numbers to be the last and the new Fibonacci number.

C Program to Print Fibonacci Numbers Up to a Given Number

#include 

void printFibonacciUptoN(int n) {
    int f1 = 0, f2 = 1;

    if (n < 1)
        return;
    printf("%d ", f1);
    while (f2 <= n) {
        printf("%d ", f2);
        int next = f1 + f2;
        f1 = f2;
        f2 = next;
    }
}

int main() {
    int n;
    printf("Enter the maximum number up to which Fibonacci numbers to print: ");
    scanf("%d", &n);
    printFibonacciUptoN(n);
    return 0;
}

In this code, the function printFibonacciUptoN(int n) takes an integer ‘n’ as input and prints all Fibonacci numbers up to ‘n’. The while loop in this function calculates the next Fibonacci number and checks whether it is less than or equal to ‘n’. If it is, the loop continues; otherwise, the loop ends. Like in the previous function, the last two Fibonacci numbers are updated after each iteration.

Please remember to compile and run your C programs in a suitable environment, like GCC or any IDE that supports the C language.

C Program to Print Fibonacci Numbers with Recursive Functions

In addition to the iterative methods above, we can generate the Fibonacci sequence using recursion. In recursion, a function calls itself until a base condition is met.

In the Fibonacci series, the base conditions are:

  • If ‘n’ is 0, the function returns 0.
  • If ‘n’ is 1, the function returns 1.

For other ‘n’, the function returns the sum of the (n-1)th and (n-2)th Fibonacci number.

Here is a C program to generate the Fibonacci series using recursion:

#include 

int fibonacci(int n) {
    if (n <= 1)
        return n;
    return fibonacci(n-1) + fibonacci(n-2);
}

void printFibonacciNumbers(int n) {
    for (int i = 0; i < n; i++)
        printf("%d ", fibonacci(i));
}

int main() {
    int n;
    printf("Enter the total number of Fibonacci numbers to print: ");
    scanf("%d", &n);
    printFibonacciNumbers(n);
    return 0;
}

In this program, the fibonacci(int n) function is a recursive function that returns the ‘n’th Fibonacci number. The printFibonacciNumbers(int n) function calls this recursive function for each ‘i’ from 0 to ‘n-1’ and prints the ‘i’th Fibonacci number.

Note: Recursion is not the most efficient way to generate a Fibonacci series for large ‘n’ because it involves repeated computation of lower Fibonacci numbers. For larger sequences, it’s better to use an iterative method or memoization with recursion.

Conclusion

The Fibonacci sequence is an essential concept in computer science and mathematics. There are many ways to generate this sequence in C programming, two of which we’ve detailed above. You can further modify and optimize these functions as per your requirement. Happy coding!

The post C Programs to Print Fibonacci Sequence appeared first on TecAdmin.

Programming Tips & Tricks TecAdmin