Fibonacci Sequence in C++

This program generates the Fibonacci sequence up to a given number of terms.

#include <iostream>

int main() {

    int n, t1 = 0, t2 = 1, nextTerm = 0;

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

    std::cin >> n;


    std::cout << "Fibonacci Series: " << t1 << ", " << t2 << ", ";

    nextTerm = t1 + t2;


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

        std::cout << nextTerm << ", ";

        t1 = t2;

        t2 = nextTerm;

        nextTerm = t1 + t2;

    }

    std::cout << "\b\b " << std::endl;  // Remove the last comma and space

    return 0;

}


No comments:

Post a Comment

MS Excel Logical Functions

Logical functions in Excel are powerful tools that help you make decisions based on conditions. Whether you're comparing values or testi...

Post Count

Loading...