0% found this document useful (0 votes)
4 views18 pages

W12 Programming Fundamentals

The document provides an introduction to recursion in C++, explaining its definition, structure, and key components such as base and recursive cases. It includes examples of recursive functions for calculating factorials, printing numbers in reverse, summing natural numbers, and converting decimal to binary, among others. Additionally, it discusses the advantages and disadvantages of using recursion in programming.

Uploaded by

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

W12 Programming Fundamentals

The document provides an introduction to recursion in C++, explaining its definition, structure, and key components such as base and recursive cases. It includes examples of recursive functions for calculating factorials, printing numbers in reverse, summing natural numbers, and converting decimal to binary, among others. Additionally, it discusses the advantages and disadvantages of using recursion in programming.

Uploaded by

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

PROGRAMMING

FUNDAMENTALS

Recursion in C++
Introduction to Recursion:
Definition:
Recursion is a programming technique where a function
calls itself to solve a smaller part of a larger problem.
Real Life Analogy:
Imagine standing between two mirrors – the reflection repeats infinitely. That’s recursion!
Another analogy: Calculating factorial(5):
5! = 5 × 4 × 3 × 2 × 1
Notice how:
5! = 5 × 4!,
4! = 4 × 3!,
and so on…
Recursive Function Call in C++:
Basic Structure:
returnType functionName(parameters) {
if (base_case_condition) {
// Stop calling itself
return some_value;
} else {
// Recursive call
return functionName(smaller_problem);
}
}
Recursive Function Call in C++:
Two Important Parts:
Base Case – The stopping condition (very important, or it loops forever!)
Recursive Case – The function calling itself with smaller input.
Example: Factorial using Recursion
#include<iostream> int main() {
using namespace std; int num = 5;
int factorial(int n) { cout << "Factorial of " << num << " is " <<
factorial(num);
if (n == 0 || n == 1) // Base case
return 0;
return 1;
}
else
Output:
return n * factorial(n - 1); // Recursive
call Factorial of 5 is 120
}
Example: Print Numbers in Reverse using Recursion

int main() {
#include<iostream>
printReverse(5);
using namespace std;
return 0;
void printReverse(int n) {
}
if (n == 0) return; // Base case
Output:
cout << n << " ";
54321
printReverse(n - 1); // Recursive call
}
Example: Sum of Natural Numbers
#include<iostream> int main() {
using namespace std; int num = 5;
int sum(int n) { cout << "Sum = " << sum(num);
if (n == 0) return 0;
return 0; }
else Output:
return n + sum(n - 1); // Recursive call Sum = 15
}
Recursion in C++
Advantages of Recursion Disadvantages of Recursion
Simplifies complex problems Uses more memory (function call
stack)
Cleaner and shorter code for
problems like tree traversal, Slower due to overhead of
factorial, etc. function calls
CLASS ACTIVITY
Problem: Print numbers from 1 to N using
recursion.
#include<iostream> int main() {

using namespace std; printNumbers(5);

void printNumbers(int n) { return 0;

if (n == 0) }

return; Output:

printNumbers(n - 1); // Go down first 12345

cout << n << " ";

}
Problem: Calculate a^b using recursion
(e.g. 2^3 = 8).
#include<iostream> int main() {

using namespace std; int base = 2, exponent = 3;

int power(int a, int b) { cout << base << "^" << exponent <<
" = " << power(base, exponent);
if (b == 0)
return 0;
return 1;
}
else
Output:
return a * power(a, b - 1);
2^3 = 8
}
Problem: Count how many digits are in a number
using recursion.

#include<iostream> int main() {

using namespace std; int num = 12345;

int countDigits(int n) { cout << "Number of digits: " <<


countDigits(num);
if (n == 0)
return 0;
return 0;
}
return 1 + countDigits(n / 10);
Output:
}
Number of digits: 5
Problem: Convert decimal number to binary
using recursion.
#include<iostream> int main() {

using namespace std; int num = 13;

void decimalToBinary(int n) { cout << "Binary of " << num << " is ";

if (n == 0) decimalToBinary(num);

return; return 0;

decimalToBinary(n / 2); }

cout << n % 2; Output:

} Binary of 13 is 1101
Problem: Use recursion to print even numbers from 1
to N.
#include<iostream> int main() {

using namespace std; int num = 10;

void printEven(int n) { cout << "Even numbers from 1 to " <<


num << ": ";
if (n == 0)
printEven(num);
return;
return 0;
printEven(n - 1);
}
if (n % 2 == 0)
Output:
cout << n << " ";
Even numbers from 1 to 10: 2 4 6 8 10
}
Problem: Find the smallest number in an
array using recursion.
#include<iostream> int main() {

using namespace std; int arr[] = {10, 5, 20, 3, 8};

int findMin(int arr[], int n) { int size = 5;

if (n == 1) cout << "Minimum element is: " <<


findMin(arr, size);
return arr[0];
return 0;
return min(arr[n - 1], findMin(arr, n -
1)); }

} Output:

Minimum element is: 3


Problem: Find the largest number in an array
using recursion
int main() {
#include<iostream> int arr[] = {7, 22, 14, 9, 30};
using namespace std; int size = 5;
int findMax(int arr[], int n) { cout << "Maximum element is: " <<
findMax(arr, size);
if (n == 1)
return 0;
return arr[0];
}
return max(arr[n - 1], findMax(arr, n -
1)); Output:
} Maximum element is: 30
Problem: Print each character of a string on a
new line using recursion.
#include<iostream> printChars(word);

using namespace std; return 0;

void printChars(string str, int index = 0) { }

if (index == str.length()) Output:

return; h

cout << str[index] << endl; e

printChars(str, index + 1); l

} l

int main() { o

string word = "hello";

You might also like