65.2k views
1 vote
Write a C++ recursive function PrintPattern to print following pattern using recursion. No loops allowed whatsoever, and you can write maximum two functions apart from main function. For example, calling your function with these argument PrintPattern \( \left(5,{ }^{\prime}{ }^{\prime},\right \), should print following pattern. Your function prototype must be as follows recursive function. void PrintPattern(int value, char ch);

1 Answer

1 vote

Final answer:

To print the given pattern using recursion in C++, you can define a recursive function called PrintPattern. In each recursive call, the function decrements the value by 1 and prints a character. The recursion stops when the value becomes 0. The given example code and explanation demonstrate how to achieve this.

Step-by-step explanation:

To print the given pattern using recursion, we can define a recursive function called PrintPattern that takes two parameters: an integer value and a character ch.

In each recursive call, we decrement the value by 1 and print the character ch. If the value becomes 0, we stop the recursion. Otherwise, we recursively call the PrintPattern function with the decremented value and the same character.

Here is the C++ code for the PrintPattern function:

#include <iostream>

PrintPattern(value - 1, ch);

return 0;

**

User ThangLeQuoc
by
7.5k points