• Articles
  • C++ class for generate Fibonacci series
Published by
Aug 9, 2016 (last update: Aug 9, 2016)

C++ class for generate Fibonacci series

Score: 3.6/5 (723 votes)
*****

Fibonacci class

The Fibonacci sequence is named after italian mathematician Leonardo of Pisa, known as Fibonacci. His 1202 book "Liber Abaci" introduced the sequence to Western European mathematics, althoutgh the sequence had been described earlier as Virahanka numbers in Indian mathematics. By convention, the sequence begin either with Fo=0 or with F1=1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>

using namespace std;

class Fibonacci{
public:
    int a, b, c;
    void generate(int);
};

void Fibonacci::generate(int n){
    a = 0; b = 1;
    cout << a << " " <<b;
    for(int i=1; i<= n-2; i++){
        c = a + b;
        cout << " " << c;
        a = b;
        b = c;
    }
}

int main()
{
    cout << "Hello world! Fibonacci series" << endl;
    cout << "Enter number of items you need in the series: ";
    int n;
    cin  >> n;
    Fibonacci fibonacci;
    fibonacci.generate(n);
    return 0;
}

Attachments: [main.cpp]