Sabtu, 06 Oktober 2012

fibonanci rekursih dan iteratif

fibonanci rekursif


#include <cstdlib>
#include <iostream>

using namespace std;

int recursiveFibo (int n)
{
if (n == 1 || n == 2)
return 1;
else
return recursiveFibo(n-1) + recursiveFibo(n-2);
}



fibonanci iteratif

#include <cstdlib>
#include <iostream>

using namespace std;

int iterativeFibo (int n)
{
if (n == 1 && n == 2)
return 1;
else
{
int f1 = 1;
int f2 = 1;

int fn;

for (int i = 3; i <= n; i++)
{
fn = f1 + f2;
f1 = f2;
f2 = fn;
}

return fn;
}
}
    //system("PAUSE");
    //return EXIT_SUCCESS;
//}

Tidak ada komentar:

Posting Komentar