recursive function

In the world of programming, recursion is implemented in a function that calls itself. Examples of such a recursive function is a function of rank, factorial, and Fibonacci sequence. In the rank function of xy, we know that all numbers other than 0, if raised to the 0 value is equal to 1.

If x raised to y, with y greater than 0, then the result is equal to x multiplied by x raised to y – 1.

xy = 1, jika y = 0
xy = x * x(y-1)
, jika y > 0
Untuk x = 10 dan y = 0, hasil dari xy adalah 1.
Untuk x = 10 dan y = 3

program C++

contoh program
fungsi fibonachi

#include

int fibonacci (int n)
{ if ((n == 1) || (n == 2)) return(1);
else return(fibonacci(n-1) + fibonacci(n-2));
}

main() {
int i, n;
cout << “Sampai suku ke : “; cin >> n;
for (i = 1; i <= n; i++) cout << fibonacci(i) << ” “;
return 0;
}

Dimana n,i=integer
n-1 = Pemanggil rekursi
i <= n= sebagai Penyetop

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: