Kumpulan Program Sederhana

Minggu, 24 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 batasangka yang anda inginkan : ";
   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 batas angka yang anda inginkan : ";
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.



Tidak ada komentar:

Posting Komentar