Fibonacci Series Program: In C, C++, Java, JavaScript, and Python

Fibonacci Series Program | Introduction

Fibonacci Series Program. The Fibonacci series is a well-known sequence in mathematics and computer science. Each number in the series is the sum of the two preceding ones, typically starting with 0 and 1. In this article, we will walk through the process of generating the Fibonacci series for a given number using various programming languages. We will cover five programming languages: C, C++, Java, JavaScript, and Python.

Fibonacci Series Program: Understand it in a Six-Step Strategy

Step 1: Understand the Problem

Before we start writing code, let’s understand the problem. We need to generate the Fibonacci series up to a certain number of terms as specified by the user.

Step 2: Design Test Cases

To ensure our code works correctly, let’s design test cases for different scenarios:

  1. Generating Fibonacci series up to 5 terms.
  2. Generating Fibonacci series up to 10 terms.
  3. Generating Fibonacci series up to 0 terms (edge case).

Step 3: Derive the Solution (Pseudocode)

Let’s outline the solution in pseudocode before implementing it in code:

Function generateFibonacci(n):
    if n <= 0:
        return empty list

    Initialize fibonacci list with [0, 1]
    While length of fibonacci list < n - 2:
        next_num = sum of last two elements in the fibonacci list
        Append next_num to the fibonacci list

    return fibonacci list

Step 4: Test the Solution

Before implementing the solution in multiple programming languages, let’s manually verify the pseudocode against the test cases:

  • generateFibonacci(5) should return [0, 1, 1, 2, 3]
  • generateFibonacci(10) should return [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]
  • generateFibonacci(0) should return []

Step 5: Write the Code

Fibonacci Series Program

Now, let’s implement the solution in various programming languages.

C
#include <stdio.h>

void generateFibonacci(int n) {
    if (n <= 0) {
        return;
    }

    int fib[2] = {0, 1};
    printf("%d %d ", fib[0], fib[1]);

    for (int i = 2; i < n; i++) {
        int next_num = fib[i - 1] + fib[i - 2];
        fib[i % 2] = next_num;
        printf("%d ", next_num);
    }
}

int main() {
    int n = 10;
    generateFibonacci(n);
    return 0;
}
C

C++
#include <iostream>

void generateFibonacci(int n) {
    if (n <= 0) {
        return;
    }

    int fib[2] = {0, 1};
    std::cout << fib[0] << " " << fib[1] << " ";

    for (int i = 2; i < n; i++) {
        int next_num = fib[i - 1] + fib[i - 2];
        fib[i % 2] = next_num;
        std::cout << next_num << " ";
    }
}

int main() {
    int n = 10;
    generateFibonacci(n);
    return 0;
}
C++

Java
public class Fibonacci {
    public static void generateFibonacci(int n) {
        if (n <= 0) {
            return;
        }

        int[] fib = {0, 1};
        System.out.print(fib[0] + " " + fib[1] + " ");

        for (int i = 2; i < n; i++) {
            int nextNum = fib[i - 1] + fib[i - 2];
            fib[i % 2] = nextNum;
            System.out.print(nextNum + " ");
        }
    }

    public static void main(String[] args) {
        int n = 10;
        generateFibonacci(n);
    }
}
Java

JavaScript
function generateFibonacci(n) {
    if (n <= 0) {
        return;
    }

    let fib = [0, 1];
    process.stdout.write(fib[0] + " " + fib[1] + " ");

    for (let i = 2; i < n; i++) {
        let nextNum = fib[i - 1] + fib[i - 2];
        fib[i % 2] = nextNum;
        process.stdout.write(nextNum + " ");
    }
}

const n = 10;
generateFibonacci(n);
JavaScript

Python
def generate_fibonacci(n):
    if n <= 0:
        return

    fib = [0, 1]
    print(fib[0], fib[1], end=" ")

    for i in range(2, n):
        next_num = fib[i - 1] + fib[i - 2]
        fib[i % 2] = next_num
        print(next_num, end=" ")

n = 10
generate_fibonacci(n)
Python

Step 6: Test the Code

After implementing the code in each language, you can run the respective program for the test cases. Ensure that the generated Fibonacci series matches your expectations based on the test cases.

In conclusion, generating a Fibonacci series involves a simple algorithm that demonstrates iterative calculations. By systematically approaching the problem, designing test cases, and implementing code in different programming languages, you can enhance your coding skills and problem-solving abilities.

Fibonacci Series Program Conclusion

The systematic problem-solving approach employed here demonstrates the importance of understanding the problem, creating meaningful test cases, developing clear pseudocode, implementing code, and rigorously testing it. By applying these practices, you can enhance your problem-solving skills and effectively tackle a wide range of programming challenges involving mathematical computations.



Leave a Comment