Write a recursive method printStarPattern() that takes a nonnegative integer as a parameter and generates the following pattern of stars.
Ex: If the nonnegative integer is 4, then the pattern generated is:

****
***
**
*
*
**
***
****

Also, write a program that prompts the user to enter the number of lines in the pattern and uses the recursive function to generate the pattern. For example, specifying 4 as the number of lines generates the above pattern.

Answer :

MrRoyal

Answer:

1. Without Recursives, Write a program to print pattern of *

#include<iostream>

// Comments are used for explanatory purpose

int main()

{

// Declare integer number, Num

int Num;

cout<<"Pattern Number: ";

cin>>NUM;

// Print * in descending order

for(j = Num; j<0;j--)

{

for(i=Num;i >i-1;i--)

{

// Print *

cout<<"*";

}

// Print new line

cout<"\n";

}

// Print * in ascending order

for(int asc=0; asc< Num; asc++)

{

for(asc2=0;asc2<=asc;asc2++)

{

// Print *

cout<<"*";

}

// Print new line

cout<"\n";

}

return 0;

}

// End Program

2. Using Recursive

#include <iostream>

using namespace std;

void printStars(int lines);

int main()

{

// prompt the user to enter a number

int lines;

cin>> lines;

printStars(int lines);

return 0;

}

void printStars(int lines)

{

if (lines < 1)

return;

// to print the stars of a particular row

if (i <= lines)

{

cout << "* ";

// recursively print rest of the stars of the row

printPatternRecur(lines, i + 1);

}

else

{

// Enter new line

cout << endl;

// print stars

printPatternRecur(lines-1, 1);

}

}

// End Program

Read more on Brainly.com - https://brainly.com/question/15089715#readmore

Other Questions