Question #37452

Write a program that prints the following elements of this serial
1 1 2 3 5 8 13 21 34 55

Definition of this sequence: this serial collects number with two numbers before.

With my pleasure if you help me

Expert's answer

#include <iostream>
using namespace std;
int main(int argc, char const *argv[])
{
    int next = 1,
        now = 1,
        before = 0;
    cout << now << ' ' << next << ' ';
    for(int i = 0; i < 8; i++)
    {
        int temp = now;
        next += now;
        now += before;
        before = temp;
        cout << next << ' ';
    }
    cout << endl;
    cin.get(); cin.get();
    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS