Program Deret Fibonacci secara Rekursif dengan Bahasa C++
#include <iostream>
using namespace std;
int Fibonacci(int n){
int a, b, c, i;
if(n==0){
return 0;
}
else if (n==1){
return 1;
}
else{
a=0;
b=1;
i=2;
while (i<=n){
c=a+b;
a=b;
b=c;
i=i+1;
}
return c;
}
}
int main() {
int i, n;
cout<<"masukkkan deret fibonacci : ";cin>>n;
cout<<endl;
cout<<"deret fibonacci : ";
for(i=0; i<n; i++){
cout<<Fibonacci(i)<<" ";
}
return 0;
}
using namespace std;
int Fibonacci(int n){
int a, b, c, i;
if(n==0){
return 0;
}
else if (n==1){
return 1;
}
else{
a=0;
b=1;
i=2;
while (i<=n){
c=a+b;
a=b;
b=c;
i=i+1;
}
return c;
}
}
int main() {
int i, n;
cout<<"masukkkan deret fibonacci : ";cin>>n;
cout<<endl;
cout<<"deret fibonacci : ";
for(i=0; i<n; i++){
cout<<Fibonacci(i)<<" ";
}
return 0;
}
Comments
Post a Comment
-Berkomentarlah yang baik dan rapi.
-Menggunakan link aktif akan dihapus.