Kamis, 21 April 2011

Konversi dari function rekursif ke iteratif


Versi rekursif :

#include <iostream.h>
#include <conio.h>

void rekursif(int x){
   if(x>=1){
   rekursif(x-1);
   cout<<"\n"<<x;
   }
};

int main()
{
   int y;
   cout<<"Masukan batasan angka yang diinginkan : ";
   cin>>y;
   rekursif(y);

}


Versi iteratif :

#include <iostream>
#include <conio.h>

void iteratif(int x){
   for(int i=1;i<=x;i++){
   cout<<"\n"<<i;
   }
};

int main()
{
int y;
cout<<"Masukan batasan angka yang diinginkan : ";
cin>>y;
iteratif(y);

}

Analysis:
The loop above shows the numbers start with 1 and ends on the numbers fed through the keyboard. In recursive functions if we use conditioning to execute the score. And on iterative functions we use for loop to execute the score.





0 komentar:

Posting Komentar

Template by:

Free Blog Templates