Practice Top 30 C++ Programming Questions on Recursion
Q1 . Write a recursive function to calculate the factorial of a number.
Input: 5
Expected Output: 120
Q2. Write a recursive function to print the nth Fibonacci number.
Input: 7
Expected Output: 13
Q3. Write a recursive function to calculate the sum of digits of a number.
Input: 1234
Expected Output: 10
Q4. Write a recursive function to calculate base^exponent.
Input: 2, 3
Expected Output: 8
Q5. Write a recursive function to check if a string is a palindrome.
Input: "radar"
Expected Output: True
Q6. Write a recursive function to find the sum of all elements in an array.
Input: {1, 2, 3, 4, 5}
Expected Output: 15
Q7. Write a recursive function to reverse a string.
Input: "hello"
Expected Output: "olleh"
Q8. Write a recursive function to find the greatest common divisor (GCD) of two numbers.
Input: 48, 18
Expected Output: 6
Q9. Write a recursive function to print the binary representation of a number.
Input: 10
Expected Output: 1010
Q10. Write a recursive function to calculate the length of a string.
Input: "recursion"
Expected Output: 9
Q11. Write a recursive function to calculate the sum of the first N natural numbers.
Input: 10
Expected Output: 55
Q12. Write a recursive function to find the smallest element in an array.
Input: {3, 1, 4, 1, 5}
Expected Output: 1
Q13. Write a recursive function to count the number of digits in a number.
Input: 4567
Expected Output: 4
Q14. Write a recursive function to check if an array is sorted in ascending order.
Input: {1, 2, 3, 4, 5}
Expected Output: True
Q15. Write a recursive function to print numbers from N to 1 in reverse order.
Input: 5
Expected Output: 5 4 3 2 1
Q16. Write a recursive function to calculate combinations using the formula:
\[
nCk = \frac{n!}{k!(n-k)!}
\]
Input: 5, 2
Expected Output: 10
Q17. Write a recursive function to find the largest element in an array.
Input: {7, 3, 9, 2}
Expected Output: 9
Q18. Write a recursive function to convert a decimal number to its octal representation.
Input: 63
Expected Output: 77
Q19. Write a recursive function to count occurrences of a specific number in an array.
Input: {1, 2, 3, 2, 2}, 2
Expected Output: 3
Q20. Write a recursive function to find the HCF of two numbers using the Euclidean algorithm.
Input: 24, 36
Expected Output: 12
Q21. Write a recursive function to calculate a^b using efficient exponentiation by squaring.
Input: 3, 4
Expected Output: 81
Q22. Write a recursive function to check whether a number is prime.
Input: 29
Expected Output: True
Q23. Write a recursive function to convert a numeric string to an integer.
Input: "1234"
Expected Output: 1234
Q24. Write a recursive function to replace all occurrences of a character in a string.
Input: "hello", 'l', 'x'
Expected Output: "hexxo"
Q25. Write a recursive function to convert a decimal number to its hexadecimal representation.
Input: 255
Expected Output: FF
Q26. Write a recursive function to calculate the sum of all even numbers in an array.
Input: {1, 2, 3, 4, 5}
Expected Output: 6
Q27. Write a recursive function to find the second largest element in an array.
Input: {7, 5, 3, 1}
Expected Output: 5
Q28. Write a recursive function to print all subsequences of a given string.
Input: "abc"
Expected Output: "a", "b", "c", "ab", "bc", "ac", "abc"
Q29. Write a recursive function to check whether a number is a perfect number.
Input: 6
Expected Output: True
(Explanation: 6 = 1 + 2 + 3)
Q30. Write a recursive function to reverse an array.
Input: {1, 2, 3, 4}
Expected Output: {4, 3, 2, 1}
Bikki Singh
Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!