c-plus-plus
  1. c-plus-plus-fibonacci-series

Fibonacci Series in C++

The Fibonacci series is a sequence of numbers where each number is the sum of the previous two numbers, starting with 0 and 1. In C++, we can generate the Fibonacci series using loops or recursion.

Syntax

Using loops

int n = 10; // number of terms to generate
int first = 0, second = 1, next;

for (int i = 0; i < n; i++) {
    if (i <= 1) {
        next = i;
    } else {
        next = first + second;
        first = second;
        second = next;
    }
    // print or store the next term in the series
}

Using recursion

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

Example

Using loops

#include <iostream>
using namespace std;

int main() {
    int n = 10; // number of terms to generate
    int first = 0, second = 1, next;

    for (int i = 0; i < n; i++) {
        if (i <= 1)            next = i;
        } else {
            next = first + second;
            first = second;
            second = next;
        }
        cout << next << " ";
    }

    return 0;
}

Using recursion

#include <iostream>
using namespace std;

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

int main() {
    int n = 10; // number of terms to generate

    for (int i = 0; i < n; i++) {
        cout << fibonacci(i) << " ";
    }

    return 0;
}

Output

Using loops

0 1 1 2 3 5 8 13 21 34

Using recursion

0 1 1 2 3 5 8 13 21 34

Explanation

In the above examples, we have generated the Fibonacci series of the first 10 terms (0, 1, 1, 2, 3, 5, 8, 13, 21, 34).

The first example uses a loop to generate the series. Inside the loop, we check if we are generating the first or second term. If it is the first or second term, we simply assign its value to "next". Otherwise, we calculate the sum of the previous two terms and store it in "next". We also update the values of "first" and "second" for the next iteration.

The second example uses recursion to generate the series. We define a function called "fibonacci" which takes an integer "n" (the nth term in the series) as input and outputs the corresponding value. The function calculates the value of the nth term by recursively calling itself with the values of (n-1) and (n-2), until it reaches the base case (n=0 or n=1).

Use

The Fibonacci series has many applications in various fields such as mathematics, science, and finance. In programming, it is often used to generate algorithms that involve a series of steps, such as sorting and searching. It can also be used to create aesthetically pleasing designs in graphics programming.

Important Points

  • The Fibonacci series is a sequence of numbers where each number is the sum of the previous two numbers, starting with 0 and 1.
  • We can generate the Fibonacci series using loops or recursion.
  • The series can be used in various fields such as mathematics, science, and finance.
  • In programming, it is often used to generate algorithms and create designs in graphics programming.

Summary

To summarize, the Fibonacci series is a sequence of numbers where each number is the sum of the previous two numbers. We can generate the series in C++ using loops or recursion. The series has many applications in different fields and can be used to generate algorithms and create designs in graphics programming.

Published on: