Here, as per the given problem pattern, a recursive approach is required to display.
Recursive functionis a function that calls itself n times. There can be n recursive functions in the program. The problem with recursive functions is their complexity. The Chinese translation of
START Step 1 -> function int printpattern(int n) If n>0 Printpattern(n-1) Print * End IF End Step 2 -> function int pattern(int n) If n>0 pattern(n-1) End IF Printpattern(n) PrintEnd STOP
#includeint printpattern(int n) { if(n>0) { printpattern(n-1); printf("*"); } } int pattern(int n) { if(n>0) { pattern(n-1); //will recursively print the pattern } printpattern(n); //will reduce the n recursively. printf(" "); //for new line } int main(int argc, char const *argv[]) { int n = 7; pattern(n); return 0; }
If we run the above program, it will generate The following output.
* ** *** **** ***** ****** *******
The above is the detailed content of Print the given pattern recursively. For more information, please follow other related articles on the PHP Chinese website!