Technical Articles

Fibonacci series

0 0
Before start the programming you need to know exactly what is the Fibonacci series, why it comes around the world??
The Fibonacci series is a sequence of numbers where each number is the sum of the two preceding ones. It starts from 0 and 1, so the sequence looks like this:

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

Rule: Fibonacci(n) = Fibonacci(n-1) + Fibonacci(n-2)

The first two numbers of the Fibonacci series are always 0 and 1. Every subsequent number is the sum of the previous two.

Golden mean:

Real-World Analogy:

Think of the Fibonacci series like the growth of a tree. You start with a small seedling (0) and a sapling (1). As they grow, the tree expands by adding branches based on how many branches existed in the previous two stages. The number of branches grows rapidly over time, just like the Fibonacci sequence grows from 0 and 1.

Fibonacci Series in Different Languages

1. Fibonacci Series in C

Here’s how you can write the Fibonacci series using iteration in C:


#include <stdio.h>

int main() {
    int n, first = 0, second = 1, next;
    
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    
    printf("Fibonacci Series: %d, %d", first, second);
    
    for (int i = 2; i < n; i++) {
        next = first + second;
        first = second;
        second = next;
        printf(", %d", next);
    }
    
    return 0;
}

Explanation:

  • We start with two variables first and second initialized to 0 and 1.
  • In the loop, we calculate the next number as the sum of the two previous numbers.
  • The loop continues for n iterations to print the Fibonacci series.

2. Fibonacci Series in C++

Here’s a C++ version of the Fibonacci series using the same iterative approach:


#include <iostream>
using namespace std;

int main() {
    int n, first = 0, second = 1, next;
    
    cout << "Enter the number of terms: ";
    cin >> n;
    
    cout << "Fibonacci Series: " << first << ", " << second;
    
    for (int i = 2; i < n; i++) {
        next = first + second;
        first = second;
        second = next;
        cout << ", " << next;
    }
    
    return 0;
}

Explanation:

  • This version is nearly identical to the C version but uses cout and cin for input/output instead of printf and scanf.
  • It follows the same logic for generating the Fibonacci series.

3. Fibonacci Series in C#

Here’s how you can implement the Fibonacci series in C#:


using System;

class FibonacciSeries {
    static void Main() {
        int n, first = 0, second = 1, next;
        
        Console.Write("Enter the number of terms: ");
        n = int.Parse(Console.ReadLine());
        
        Console.Write("Fibonacci Series: " + first + ", " + second);
        
        for (int i = 2; i < n; i++) {
            next = first + second;
            first = second;
            second = next;
            Console.Write(", " + next);
        }
    }
}

Explanation:

  • In C#, we use Console.WriteLine() and Console.ReadLine() for input and output.
  • int.Parse() is used to convert the input string into an integer.
  • The rest of the logic is the same as in C and C++, following the pattern of adding the two previous numbers to get the next.

Real-World Example Expanded:

Imagine you’re planting a tree, and each year it grows more branches. In the first year, it grows one branch (like the first 1 in the Fibonacci series). In the second year, it grows another branch. But in the third year, the tree “remembers” how many branches it had in the past two years (1 + 1) and grows that many new branches, giving it 2 new branches. In the fourth year, it adds the branches from the previous two years (1 + 2), giving it 3 new branches. Over time, the number of new branches grows according to the Fibonacci sequence, just like in this program.

By understanding the Fibonacci series in both code and real life, you’re looking at a pattern that’s found in nature (like plant growth) and also a fundamental concept in programming and mathematics.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
× How can I help you?