top of page
Caută
  • Poza scriitoruluioanaunciuleanu

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>
using namespace std;

void fibonacci(int number)
{
    int firstNumber = 0;
    int secondNumber = 1;
    int limit = 1;
    int nextNumber;

    cout << "First " << number << " elements of the Fibonacci series are :" << endl;

   for (int i = 0 ; i < number ; i++)
   {
      if (i <= limit)
      {
          nextNumber = i;
      }
      else
      {
         nextNumber = firstNumber + secondNumber;
         firstNumber = secondNumber;
         secondNumber = nextNumber;
      }
      cout << nextNumber << endl;
    }
}

main()
{
    int number;
    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

40 afișări0 comentarii

Postări recente

Afișează-le pe toate

Weighted Job Scheduling in JAVA

You receive a list of jobs that have three elements: start time, finish time and profit. You have to schedule the jobs with no overlapping, in order to obtain the maximum profit. Solution: 1. static

Tiling Problem in JAVA

You can use a board that is 2 x n size. The tiles are of size 2 x 1. Count the number of ways to tile the board. Tiles can be placed vertically 2 x 1 or horizontally as 1 x 2. Solution: 1. static

bottom of page