Write a C++ recursive function PrintPattern1 to print following pattern using recursion. No
loops allowed whatsoever, and you can write other helping (recursive) functions. For example, calling your function with these argument PrintPattern1(1,10) should print following pattern. Your function prototype must be as follows:
void PrintPattern1(int start, int end);
pattern:
*
*
*
*
*
*
*
#include <iostream>
using namespace std;
void PrintPattern3(int start, int end) {
for (int i = 1; i < 2 * end; i++) {
if (i == start || i == 2 * end - start) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
if (start < end) {
PrintPattern3(start + 1, end);
for (int i = 1; i < 2 * end; i++) {
if (i == start || i == 2 * end - start) {
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
}
int main()
{
PrintPattern3(1, 5);
return 0;
}
Comments
Leave a comment