- oanaunciuleanu

# Fibonacci Series in C++

The Fibonacci series is a sequence of numbers where one item is the sum of the previous two numbers. The first 2 numbers are 0 and 1, so their sum is 1 and the third element in the row is 1. Now we add 1 and 1 and we get 2, which is the next number. The series is infinite, and it just follows this simple rule.

// Fibonacci series #include<iostream>usingnamespacestd;voidfibonacci(intnumber) {intfirstNumber =0;intsecondNumber =1;intlimit =1;intnextNumber; cout << "First " << number << " elements of the Fibonacci series are :" << endl;for(inti =0; i < number ; i++) {if(i <= limit) { nextNumber = i; }else{ nextNumber = firstNumber + secondNumber; firstNumber = secondNumber; secondNumber = nextNumber; } cout << nextNumber << endl; } } main() {intnumber; cout << "How many elements from the Fibonacci series do you want to see?" << endl; cin >> number; fibonacci(number); }

First 10 elements of the Fibonacci series are: 0 1 1 2 3 5 8 13 21 34

16 afișare