This document provides examples of using recursion to calculate factorial numbers and convert numbers to their binary representations. It explains that recursion involves defining a problem in terms of simpler versions of itself, with a base case that can be solved directly. For factorial, the base case is 0! = 1, and larger numbers are calculated as n! = n * (n-1)!. For binary conversion, the base case is when the number equals 1 (output 1), and larger numbers are converted by recursively processing the number divided by 2 and outputting the remainder.
1. Recursion Examples:
1. Factorial
2. Binary representation
1. Calculating Factorial of a Number:
factorial function(n!): the product of the integers between 1 and n.
In other words multiply all whole numbers from our chosen number down to 1.
➢ Code:
Using Loop Using Recursion
int main()
{
int num;cin >> num;
int factorial = 1;
for (int i = 1; i <= num; ++i)factorial *=
i;
cout << factorial;
return 0;
}
int factorial(int num) {
if (num == 0)return 1;
else return num * factorial(num - 1);
}
int main()
{
int num;cin >> num;
cout << factorial(num);
return 0;
}
➢ Behind the scenes:
n!=n*(n-1)!
4! = 4 × 3 × 2 × 1 = 24
1! = 1
int factorial(int 3)
{
if (0 == 0)return 1;
else
return0*factorial(-
1);
}
num =0
int factorial(int 3)
{
if (1 == 0)return 1;
else
return1*factorial(0);
}
num =1
int factorial(int 3)
{
if (2 == 0)return 1;
else
return2*factorial(1);
}
num =2
int factorial(int 3)
{
if (3 == 0)return 1;
else
return3*factorial(2);
}
num=3
int factorial(int 4)
{
if (4 == 0)return 1;
else
return4*factorial(3);
}
num =4
Key :
Red: False -Not executed
Green: True - Executed
2. Important Note:
when a function calls itself, a new copy of that function is run. The local variables in the second version are
independent of the local variables in the first, and they cannot affect one another directly.
➢ Steps:
1. Identify the basic cases (those in which the subprogram can solve the problem directly without recurring to
recursive calls) and determine how they are solved.
For example, in the case of factorial, the only basic case used in the function is n=0. Similarly, we could have
considered a more general basic case (e.g., n ≤ 1). In both cases, the function should return 1.
2. Determine how to resolve the non-basic cases in terms of the basic cases, which we assume we can already
solve.
In the case of a factorial, we know that the factorial of a number n greater than zero is n*factorial(n-1).
3. Make sure that the parameters of the call move closer to the basic cases at each recursive call. This should
guarantee a finite sequence of recursive calls that always terminates.
In the case of a factorial, n-1 is closer to 0 than n. Therefore, we can guarantee that this function terminates.
2. Writing Binary Representation of a Number:
➢ Code
Using Loop Using Recursion
int binary[32];
int main()
{
int num;cin >> num;
int i = 0;
for (; 0 < num; ++i) {
binary[i] = num % 2;
num /= 2;
}
for (int j = i - 1; j >= 0; j--) {
cout << binary[j];
}
return 0;
}
void binary(int num)
{
if (num == 1) cout << 1;
else {
binary(num / 2);
cout << num % 2;
}
}
int main()
{
int num; cin >> num;
binary(num);
return 0;
}
1
3*2=6
1*2=2
1*1=1
4*6=24
4!= 4*3!
3!=3*2!
2!=2*1!
1!=1*0!
0!=1