Fibonacci Sequence: Mathematics

Algorithms

Fibonacci Sequence

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

F(n) = F(n−1) + F(n−2)

With initial conditions:

F(0) = 0, F(1) = 1

Note: The sequence begins: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, and so on. Each subsequent number is the sum of the two preceding ones.

So, each term in the Fibonacci sequence is the sum of the two preceding terms. The sequence continues indefinitely, and the ratio of consecutive Fibonacci numbers converges to the golden ratio (ϕ), which is approximately 1.618. The Fibonacci sequence has applications in various areas, including mathematics, computer science, and nature, where it describes the arrangement of leaves on a stem, the florets of a flower, or the spirals of a pinecone.

A simple example of a C program that generates and prints the Fibonacci sequence up to a specified number of terms:

c Copy Code
#include <stdio.h>

// Function to generate and print Fibonacci sequence
void generateFibonacci(int n) {
    int first = 0, second = 1, next;

    printf("Fibonacci Sequence up to %d terms:\n", n);

    for (int i = 0; i < n; i++) {
        printf("%d, ", first);

        next = first + second;
        first = second;
        second = next;
    }
    printf("\n");
}

int main() {
    int terms;

    // Get the number of terms from the user
    printf("Enter the number of terms for Fibonacci sequence: ");
    scanf("%d", &terms);

    // Check if the input is non-negative
    if (terms < 0) {
        printf("Please enter a non-negative number.\n");
    } else {
        // Call the function to generate and print Fibonacci sequence
        generateFibonacci(terms);
    }

    return 0;
}
Output:
Enter the number of terms for Fibonacci sequence: 10
Fibonacci Sequence up to 10 terms:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34,

This program prompts the user to enter the number of terms they want in the Fibonacci sequence and then generates and prints the sequence up to the specified number of terms. The 'generateFibonacci' function uses a loop to calculate and print each term in the sequence.

What is New?

We have just updated this website & still working on adding new content & updating existing features.