Senin, 02 April 2012

fibonacci use iterative

assalamu'alaikum warahmatullah..
Alhamdulillah,,This time I wanted to try today to discuss issues about the Fibonacci numbers .... based tutorials provided.Fibonacci numbers are the numbers that have the general form f1 = 0, f2 = 1, and f3 = f1 + f2 and f (n) = f ((n-2) + (n-1))

analysis of the problem :
 Fibonacci numbers are the numbers that have the general form 0 1 1 2 3 5 .... f(n) (with f(n) =  f ((n-2) + (n-1)))

so, The first step is initiate if f1 = 0, f2 = 1, and f3 = f1 + f2 and f (n) = f ((n-2) + (n-1))
 
flowchart !






Program....
 #include <cstdlib>
#include <iostream>
#include <conio.h>
using namespace std;
class fibonacci {
      friend istream& operator>>(istream&,fibonacci&);
      friend ostream& operator<<(ostream&,fibonacci&);
    
      public :
             fibonacci(int f1=0, int f2=1, int f3=0, int n=0){
                                   angka1=f1;
                                   angka2=f2;
                                   angka3=f3;
                                   suku=n;
                                   }
      private :
              int angka1;
              int angka2;
              int angka3;
              int suku;
              };
      istream& operator>>(istream& in,fibonacci& a){
               cout<<"masukkan n = ";
               in>> a.suku;
               return in;
               }
      ostream& operator<<(ostream& out, fibonacci& b){
               b.angka1=0;
               b.angka2=1;
               b.angka3=b.angka1+b.angka2;
                out<<b.angka1<<" "<<b.angka2<<" ";
                               for(int i=1;i<=b.suku;i++){
                                       b.angka3=b.angka1+b.angka2;
                                       b.angka1=b.angka2;
                                       b.angka2=b.angka3;
                                       if (b.angka3<=b.suku){
                                       out<<b.angka3<<" ";
                                       }
                                       i++;
                                       }
               return out;
               }
 
int main(){
    fibonacci c;
    cin>>c;
    cout<<c;
    system("pause");
    return 0;
}










Tidak ada komentar:

Posting Komentar