Saturday, May 7, 2011

Fibonacci Array dengan C++

Dikasih tugas kampus sama dosen. Suruh bikin Fibonacci sama array. Ane baru denger. Fibonacci digabung sama array. Oke, let's see my little code:

#include <iostream>
#include <conio.h>
using namespace std;

long fibo(unsigned long n) {
if (n <= 1) {
return n;
} else {
return fibo(n-1)+fibo(n-2);
}
}

int main() {
long input;
long n1 = 0;
long n2 = 1;
cout<<"Enter Number : ";
cin >> input;

cout << "\n\nWith Iteration(C++) : \t0 1 ";

long fibonacci[input];
for (int i = 2; i < input; i++) {

fibonacci[i] = n1 + n2;
n1 =  n2;
n2 = fibonacci[ i ];
cout << fibonacci[i] << " ";
}

cout<<"\n\nWith Recursion(C++) : \t";
for( int i = 0; i < input; i++)
{
fibonacci[i] = fibo( i );
cout<<fibonacci[i]<<" ";
}
return *fibonacci;
getch();
}

disini ane make long biar lebih luas cakupan angka yang bisa ditampung sama program ini...

kalau ada pertanyaan, silahkan bertanya. Kalau mau copy juga boleh.

MencobaBelajar untuk masa depan yang lebih cemerlang. Bismillah....

No comments:

Post a Comment

Your Comment is Our Order, Your Majesty