In: Computer Science
Code:
#include <iostream>
using namespace std;
void print_asterisk(int asterisk)
{
// base case
if (asterisk == 0)
return;
cout << "#";
print_asterisk(asterisk - 1);
}
// function to print the upper-half of the pattern
void pattern_upper(int n, int num)
{
// base case
if (n == 0)
return;
print_asterisk(num - n + 1);
cout << endl;
// recursively calling pattern_upper()
pattern_upper(n - 1, num+1);
}
// function to print the lower-half of the pattern
void pattern_lower(int n, int num)
{
if (n == 0)
return;
print_asterisk(n);
print_asterisk(n-1);
cout << endl;
// recursively calling pattern_lower(
pattern_lower(n - 1, num-1);
}
// function to print the pattern
void pattern(int n, int num)
{
pattern_upper(n, num);
pattern_lower(n , num);
}
// driver function
int main()
{
int n = 9;
pattern((n/2)+1, (n/2)+1);
return 0;
}
Output: