Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration).
Recursion in computer science is a method where the solution to a problem depends on solutions to smaller instances of the same problem (as opposed to iteration). Recursive algorithms have two cases: a recursive case and base case. Any function that calls itseld is recursive.
Examples of recursive functions:
Recursion is useful for tasks that can be defined in terms of similar subtasks, for example search, sort , and traversal problems often have simple recursive solutions. At some point the function encounters a subtask that it can perform without calling itself.
Factorial numbers defined recursively example:
factorial(0) = 1;
factorial(n) = n * factorial(n-1);
0! = 1
1! = 1 * 1 = 1
2! = 2 * 1 * 1 = 2
3! = 3 * 2 * 1 * 1 = 6
4! = 4 * 3 * 2 * 1 * 1 = 24
/*
*This is a Tower of hanoi recursive program
* written in javascript
*This program will tell you the correct
* moves to make for any number of disks 'n'
* in this case n = 4
*/
function move(n, a, b, c) {
if (n > 0) {
move(n-1, a, c, b);
console.log("Move disk from " + a + " to " + c);
move(n-1, b, a, c);
}
}
move(4, "A", "B", "C");
Learn how to change a recursive function into a recurrence relation
Download PDFMultiplication can be thought of as a recursive function. Multiplication is simply adding the number 'X' 'Y' times or vice versa. For example if I multiplied 5 by 3 (e.g. 5 * 3) the way multiplication works, we get 5 + 5 + 5 = 15 or 3 + 3 +3+ 3+ 3= 15 both are correct ways to do multiplication. This works perfectly for positive integers, but what if the we wanted to multiply 5 * 0 = 0 or 0 * 5 =0 and 5 * 1 = 5 or 1 * 5 = 5, that will be our base case also known as the stopping or non-recursive case.
So what will the recursive program look like ? Base case if input X or input Y is 0 we will return 0, if X is 1 then we return Y , if Y is 1then we return X. Both X and Y are our input parameter variables. You look at the multiplication function next to this paragraph to see how we would multiply two positive numbers recursively. Note: you cannot use this function for negative values.
int Multiply(int X, int Y){
if( X == 0 || Y== 0)
return 0;
if(X == 1)
return Y;
if(Y == 1)
return X;
returnY + Multiply(X -1, Y);
}
In this video I show how to write a recursive power function in C programming.
â–ºSupport this channel for FREE by doing your Amazon shopping through this link (bookmark it!):
https://www.amazon.com/?tag=everythingc06-20
Get the code here:
https://github.com/randerson112358/C-Programs/blob/master/power_function_recursion.c
Learn how to use recursion to multiply two numbers
Download PDF/* C Program for a recursive fibonacci function */ #include< stdio.h > int Fibonacci(int); main() { int n, i = 0, c; scanf("%d",&n); printf("Fibonacci series\n"); for ( c = 1 ; c <= n ; c++ ) { printf("%d\n", Fibonacci(i)); i++; } return 0; } int Fibonacci(int n) { if ( n == 0 ) return 0; else if ( n == 1 ) return 1; else return ( Fibonacci(n-1) + Fibonacci(n-2) ); }