Program C++ Mencari Deret Fibonacci dengan Array


Script

#include <iostream.h>
#include <conio.h>
void main()
{
int i, n, a[50];
cout<<“===============================\n”;
cout<<“Program Pencari Deret Fibonacci\n”;
cout<<“===============================\n\n”;
cout<<“Masukkan Batas Deret : “; //menginputkan deret ke n
cin>>n;
//membuat deret
for (i=1; i<=n; i++)
{
if(i<2)
a[i]=i;
else
a[i]=a[i-2]+a[i-1];
}
//menampilkan deret
for (i=1; i<=n; i++)
{
cout<<a[i]<<” “;
}
cout<<“\n\nDeret ke “<<n<<” : “<<a[n];  //menampilkan deret ke n dan bil. fibonacci
getche ();
}

Hasil

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s