example of a recursive program that is:
Yn with n integer greater than zero can be calculated recursively by using the reference as follows:
- Yn, for n = 1
- Y X Yn-1, for n
Subroutine rank (y, n)
IF n = 1 THEN
BEHIND THE VALUE-y
OTHERWISE
VALUE BEYOND y-x rank (y, n-1)
END-IF
END-subroutine:
in program:
#include <iostream.h>
#include <conio.h>
long int pangkat (unsigned int y, unsigned int n)
{
if (n == 1)
return y;
else
return y * pangkat (y, n-1);
}
int main ()
{
int y, n;
long int hasil;
cout <<"mengitung y pangkat ^n \n";
cout <<" y =";
cin >> y;
cout<<" n =";
cin >> n;
hasil = pangkat (y,n)
cout << y <<"^" << n << " =" <<hasil;
getch ();
return 0;
}
Tidak ada komentar:
Posting Komentar