KEMBAR78
Recursion CPP | PDF | Theoretical Computer Science | Mathematical Logic
0% found this document useful (0 votes)
12 views10 pages

Recursion CPP

Recursion is a programming technique where a function calls itself to solve complex problems by breaking them down into simpler ones. There are two main types of recursion: direct (which includes tail and head recursion) and indirect recursion. Examples of recursion include calculating the sum of a range of numbers, finding the factorial of a number, and reversing a string.

Uploaded by

2207340130040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views10 pages

Recursion CPP

Recursion is a programming technique where a function calls itself to solve complex problems by breaking them down into simpler ones. There are two main types of recursion: direct (which includes tail and head recursion) and indirect recursion. Examples of recursion include calculating the sum of a range of numbers, finding the factorial of a number, and reversing a string.

Uploaded by

2207340130040
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Recursion

Introduction:
➢ Recursion is the technique or method of
making a function call itself.

➢ This provides a way to break


complicated problems down into
simple problems which are easier to
solve.
Types of Recursion:

1- Direct recursion
➢ Tail recursion
➢ Head recursion

2- In-direct recursion
1- Tail recursion:
If a recursive function calling itself and that
recursive call is the last statement in the function
then it’s known as Tail Recursion.

2- Head Recursion:
If a recursive function calling itself and that
recursive call is the first statement in the
function then it’s known as Head Recursion.
Recursion Example:
Adding a range of number without using loop.

In the following example, recursion is used to add a range of numbers


together by breaking it down into the simple task of adding two
numbers:

5
Recursion Example:
When the sum() function is called, it adds parameter n
to the sum of all numbers smaller than n and returns
the result . When n becomes 0, the function just returns 0.
When running, the program follows these steps:

10 + sum(9)
10 + ( 9 + sum(8) )
10 + ( 9 + ( 8 + sum(7) ) )
...
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
10 +9+8+7+6+5+4+3+2+1+0
Example 1:
int sum(int k) {
if (k > 0) {
return k + sum(k - 1);
} else {
return 0;
}
}

int main() {
int result = sum(10);
cout << result;
return 0;
}
Example 2:
Factorial of a Number Using Recursion
// Factorial of n = 1*2*3*...*n
#include <iostream>
int factorial(int n) {
using namespace std;
if (n > 1) {
int factorial(int);
return n * factorial(n - 1);
int main() {
} else {
int n, result;
return 1;
cout << "Enter a non-negative number: ";
}
cin >> n;
}
result = factorial(n);
cout << "Factorial of " << n << " = “<<result;
return 0;
}
8
Example 3:
Reverse a string using recursion.
#include <iostream>
int main() {
using namespace std;
int findMax(int nums[], int start, int end) {
int nums[] = {9, 2, 4, 0, 2, 2, 3, 4, 5, 7};
if (start == end) int size = sizeof(nums) / sizeof(nums[0]);
return nums[start];
cout << "Array elements: " << endl;
int mid = (start + end) / 2;
int max1 = findMax(nums, start, mid);
for (size_t i = 0; i < size; i++) {
int max2 = findMax(nums, mid + 1, end); cout << nums[i] << ' ';
return (max1 > max2) ? max1 : max2;
}
}
int findMin(int nums[], int start, int end) {
int max = findMax(nums, 0, size - 1);
if (start == end) int min = findMin(nums, 0, size - 1);
return nums[start];
cout << "\nMaximum element: " << max << endl;
int mid = (start + end) / 2;
int min1 = findMin(nums, start, mid);
cout << "Minimum element: " << min << endl;
}
int min2 = findMin(nums, mid + 1, end);
return (min1 < min2) ? min1 : min2;}
9
thank you

You might also like