Write the code to find the Fibonacci series upto the nth term.

Introduction:     

        Fibonacci series is one of the classic programming challenges. This problem not only tests a coder's fundamental understanding but also their ability to think algorithmically across different programming languages. 


What is Fibonacci series?
        The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, starting from 0 and 1.

                            Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, ...

Solutions:

Now let us see the solution in different programming languages.

1. Java

        import java.util.Scanner;

        public class FibonacciSeries {


            // Function to print Fibonacci series up to n terms

            public static void printFibonacci(int n) {

                int a = 0, b = 1, next;

                System.out.print("Fibonacci Series: ");

        

                // Loop to generate Fibonacci sequence up to n terms

                for (int i = 1; i <= n; i++) {

                    System.out.print(a + " "); // Print the current term

                    // Calculate the next term

                    next = a + b;

                    // Update a and b for the next iteration

                    a = b;

                    b = next;

                }

                System.out.println(); // Print a newline at the end

            }

            // Main Method

            public static void main(String[] args) {

                Scanner scanner = new Scanner(System.in);

                System.out.print("Enter the number of terms: ");

                int n = scanner.nextInt(); // Input number of terms


                printFibonacci(n); // Call the function to print the series

                scanner.close();

            }

        }

2. Python

        # Function to print Fibonacci series up to n terms
        def print_fibonacci(n):
            a, b = 0, 1
            print("Fibonacci Series:", end=" ")
    
            # Loop to generate Fibonacci sequence up to n terms
            for _ in range(n):
                print(a, end=" ") # Print the current term
        
                # Update a and b for the next iteration
                a, b = b, a + b  # b becomes the next term, and a + b is updated

            print() # Print a newline at the end

        # Input from user
        n = int(input("Enter the number of terms: "))
        print_fibonacci(n) # Call the function to print the series


3. C++

        #include <iostream>

        using namespace std;

        // Function to print Fibonacci series up to n terms

        void printFibonacci(int n) {

            int a = 0, b = 1, next;

            cout << "Fibonacci Series: ";

            // Loop to generate Fibonacci sequence up to n terms

            for (int i = 1; i <= n; i++) {

                cout << a << " "; // Print the current term

                // Calculate the next term

                next = a + b;

                // Update a and b for the next iteration

                a = b;

                b = next;

            }

            cout << endl; // Print a newline at the end

        }

        int main() {

            int n;

            cout << "Enter the number of terms: ";

            cin >> n; // Input number of terms

            printFibonacci(n); // Call the function to print the series

            return 0;

        }



4. C 

        #include <stdio.h>

        // Function to print Fibonacci series up to n terms

        void printFibonacci(int n) {

            int a = 0, b = 1, next;

            printf("Fibonacci Series: ");

            // Loop to generate Fibonacci sequence up to n terms

            for (int i = 1; i <= n; i++) {

                printf("%d ", a); // Print the current term

                // Calculate the next term

                next = a + b;

                // Update a and b for the next iteration

                a = b;

                b = next;

            }    

            printf("\n"); // Print a newline at the end

        }

        int main() {

            int n;

            printf("Enter the number of terms: ");

            scanf("%d", &n); // Input number of terms

            printFibonacci(n); // Call the function to print the series

            return 0;

        }

Sample Input & Output:

Input: 5

Fibonacci Series: 0 1 1 2 3


Input: 8

Fibonacci Series: 0 1 1 2 3 5 8 13

Explanation of Code:

  1. Each code version defines a printFibonacci function to generate and display the Fibonacci series up to the nth term.
  2. Loop: It iterates n times to print each term.
  3. Variable Updates: The a and b variables represent consecutive terms in the series, and we use a temporary variable next (or a tuple assignment in Python) to keep adding the previous two terms.

This code will output the Fibonacci sequence starting from 0, up to the number of terms specified by the user.


**  Please do subscribe my blog for future updates and share with your friends, if you find this informative **

Comments

Popular Posts