Recursive programming is a procedure in which a method calls itself, so that a problem is solved more and more with each method call. This continues until the problem is reduced to a very simple case.
Some tasks in computer science can be solved well by reducing a large problem step by step to smaller and smaller problems of the same kind until simple (trivial) solutions arise . From this finally the solution of the original large problem is put together.
return_type function_name(type parameter) {
// code to be executed
return function_name(type parameter)
}
#include <iostream>
using namespace std;
int factorial(int number) {
if (number < 2) {
return 1;
} else {
return number * factorial(number - 1);
}
}
int main() {
cout << "factorial of 8 is " << factorial(8);
return 0;
}
factorial of 8 is 40320
##include <iostream>
using namespace std;
int fibonacciAlgorithm(int number) {
if (number == 0 || number == 1) {
return number;
} else {
return fibonacciAlgorithm(number - 1) + fibonacciAlgorithm(number - 2);
}
}
int main() {
cout << "fibonacci number of the number 10 is " << fibonacciAlgorithm(10);
return 0;
}
fibonacci number of the number 10 is 55