How to get Fibonacci sequence in cpp

How to get Fibonacci sequence in cpp

A very straightforward way for beginners

Here is the best way to handle it

#include <iostream>

using namespace std;

void fibonacci(int n) {

int a = 0, b = 1, next;

cout << "Fibonacci Sequence: ";

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

cout << a << " ";

next = a + b;

a = b;

b = next;

}

}

int main() {

int n;

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

cin >> n;

fibonacci(n

);

return 0;

}

Yusuf Abdulqudus's Profile PicYusuf Abdulqudus