menu bar

Saturday, 9 April 2016

fibonacci dengan cara iteratif.


#include <iostream>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;
int main(int argc, char** argv) {
int n, firstTerm = 1, secondTerm = 1, nextTerm;
    cout << "masukan nilai: ";
    cin >> n;

    cout << "urutan Fibonacci: " << firstTerm << " + " << secondTerm << " + ";
    nextTerm = firstTerm + secondTerm;

    while ( nextTerm < n) {
        cout << nextTerm << " + ";
        nextTerm = firstTerm + secondTerm;
        firstTerm = secondTerm;
        secondTerm = nextTerm;
    }
return 0;
}

No comments:

Post a Comment