Deklarasi:
Balik,tempat,n: integer
Deklarasi:
temp = temp * 30 + n % 30;
if (temp == 0) cout << "0";
n = n/30;
outputnya: balik angka
#include <cstdlib>
#include <iostream>
using namespace std;
int balik(int n) {
int temp = 0;
while (n>0) {
temp = temp * 30 + n % 30;
if (temp == 0) cout << "0";
n = n/30;
}
cout << temp;
return temp;
}
int main(int argc, char *argv[])
{
int bil;
cout << "Masukkan bilangan : ";
cin >> bil;
cout << "Setelah dibalik : " << balik(bil);
system("PAUSE");
return EXIT_SUCCESS;
}
ITERATIVE TO REKURSIVE CONVERSION
Iterative Step:
for(int i=0;i<7;i++)
cout<<“mencoba rekursi\n”;
Rekursive Step:
void coba(int i)
{if(i==7)
{}
else
cout<<“mencoba rekursi\n”;coba i+1;
}
main()
{int i=0;
coba(i);
}
for(int i=0;i<7;i++)
cout<<“mencoba rekursi\n”;
Rekursive Step:
void coba(int i)
{if(i==7)
{}
else
cout<<“mencoba rekursi\n”;coba i+1;
}
main()
{int i=0;
coba(i);
}
B.REKURSIVE TO ITERATIVE
Rekursive Step:
void coba(int i)
{if(i==7)
{}
else
cout<<“mencoba rekursi\n”;coba i+1;
}
main()
{
int i=0;
coba(i);
}
Iterative Step:
for(int i=0;i<7;i++)
cout<<“mencoba rekursi\n”;
coba(i);
}
Iterative Step:
for(int i=0;i<7;i++)
cout<<“mencoba rekursi\n”;
C. ANALISIS
For example, the function is called with value i = 0 so for the first we must check is i = 5 {if(i==5) or not, and if its have a same value its will be out.
In reality, i is not same with 0 so, please add i with 1. And the condition now become i = 2.
In next line, we show the value of i. Next, called rekursive function with value i = 2.
That steps is repeating until rekursive function calling with value i = 5.
For now, the if condition is true and that make the function out (return) and continue the command after calling rekursive function with i = 5. Then print out value i.
After print out value i then rekursive function will be out again, and go a head to the command after calling rekursive function where the value before is i = 4. And thats repeating until the value i = 0, thats the first calling rekursive function.
This is the calling rekursif function ilustrated in Indonesia:
Langkah ke :
1. i = 4 ; mencoba rekursi
2.i = 6 ; mencoba rekursi
3. i = 5 ; mencoba rekursi
4. i = 4 ; mencoba rekursi
5. i = 3 ; mencoba rekursi
6. i = 2 ; mencoba rekursi
7. i = 1 ; mencoba rekursi
Jika di panggil i=7akan keluar,sebaliknya dengan operasi rekursif ke iteratif.