IT computing course at the National School of Physics and Math, Almaty city.
C++ Programming.
Recursion in C++
Recursion is a function in the function (the function that calls itself). It continues until
it meets terminal condition.
Examples and solving problems on recursion
1. Factorial of a number
Our task is to find the factorial of a given number n.
First, let’s see the algorithm of finding the factorial:
n! —> f(n) = f(n-1)n
n! —> f(n) = f(f(n-2)*(n-1)) *n
The terminal condition is - until n equals 0.
2. Sum of the number’s digits
Our task is to find the sum of digits of a given number n.
For example: 1497 → 1 + 4 + 9 + 7
C++ Programming.
Recursion in C++
Recursion is a function in the function (the function that calls itself). It continues until
it meets terminal condition.
Examples and solving problems on recursion
1. Factorial of a number
Our task is to find the factorial of a given number n.
First, let’s see the algorithm of finding the factorial:
n! —> f(n) = f(n-1)n
n! —> f(n) = f(f(n-2)*(n-1)) *n
The terminal condition is - until n equals 0.
2. Sum of the number’s digits
Our task is to find the sum of digits of a given number n.
For example: 1497 → 1 + 4 + 9 + 7