Declaration:
x, i : integer
Descripyion:
Rekursif
read x
if (x >= 1) then
rekursif (x - 1)
write x
end if
end
Iteratif
read x
for i <- 1 to x do
write i
end for
end
Program:
- #inc
- #include <iostream.h>
- lude <conio.h>
- int iteratif(int x){
- for(int i=1; i<=x; i++){
- cout<<i;
- }}
- int rekursif(int x){
- if(x>=1){
- rekursif(x-1);
- cout<<x;
- }
- }
- int main(){
- int x;
- cout<<"Inputkan angka : ";
- cin>>x;
- cout<<endl;
- cout<<"Iteratif: ";
- iteratif(x);
- cout<<endl;
- cout<<"Rekursif: ";
- rekursif(x);
- getch();
- return 0;
- }
Tidak ada komentar:
Posting Komentar