Good day, good people! Welcome to the Recursion practice exercises for CP143.
Course: CP143 - Computer Programming Institution: Stellenbosch University Topic: Recursion and Advanced Function Concepts Lecture Coverage: Lecture 12 Textbook: Chapter 5 (sections 5.13-5.16) Learning Outcomes: LO2 (Design efficient algorithms), LO5 (Control structures - recursion)
By completing these exercises, you will:
- Understand recursive problem solving (base case + recursive step)
- Implement classic recursive algorithms
- Trace recursive execution and call stacks
- Compare recursive vs iterative solutions
- Apply recursion to strings, arrays, and mathematical problems
This repository contains 5 practice exercises designed to help you master recursion in C. Each exercise focuses on different aspects of recursive thinking and implementation.
File: Exercise_01/exercise_1.c
Write recursive functions to calculate sum and product of numbers 1 to n
File: Exercise_02/exercise_2.c
Check if a string is a palindrome using recursion
File: Exercise_03/exercise_3.c
Implement linear search recursively
File: Exercise_04/exercise_4.c
Find greatest common divisor using the Euclidean algorithm
File: Exercise_05/exercise_5.c
Convert binary strings to decimal numbers recursively
Click the button below to open this repository in a fully configured cloud development environment:
Why Codespaces?
- ✅ No setup required - works in your browser
- ✅ Pre-configured C compiler and debugger
- ✅ Consistent environment across all devices
- ✅ Free tier available (60 hours/month)
- ✅ Access from anywhere with internet
Clone the repository:
git clone https://github.com/SwiftTuition/CP143-Recursion-and-Advanced-Functions.git
cd CP143-Recursion-and-Advanced-FunctionsEach exercise can be compiled using GCC:
gcc exercise_1.c -o exercise_1
gcc exercise_2.c -o exercise_2
gcc exercise_3.c -o exercise_3
gcc exercise_4.c -o exercise_4
gcc exercise_5.c -o exercise_5Or if you're in an exercise folder:
cd Exercise_01
gcc exercise_1.c -o exercise_1After compiling, run the executable:
./exercise_1
./exercise_2
./exercise_3
./exercise_4
./exercise_5On Windows:
exercise_1.exe
exercise_2.exe
exercise_3.exe
exercise_4.exe
exercise_5.exe- Identify the Base Case: Every recursive function needs a terminating condition
- Define the Recursive Step: How does the problem get smaller with each call?
- Trust the Recursion: Assume the recursive call works for smaller inputs
- Trace Your Code: Use pencil and paper to trace through recursive calls
- Test Edge Cases: Try n=0, n=1, empty strings, etc.
- Forgetting the base case (causes infinite recursion and stack overflow)
- Incorrect base case (function never terminates)
- Not making progress toward the base case
- Modifying parameters incorrectly
- Stack overflow from too many recursive calls
If you're stuck:
- Read the problem description carefully in each exercise's README.md
- Review the theory and examples from the learning package
- Trace through the recursion with small inputs (n=1, n=2, n=3)
- Check that your base case is correct
- Verify that each recursive call makes progress toward the base case
Good luck!
Swift Tuition CP143 Learning Package