Home >Backend Development >C++ >Print the given pattern recursively

Print the given pattern recursively

王林
王林forward
2023-09-17 10:13:061266browse

Print the given pattern recursively

Here, as per the given problem pattern, a recursive approach is required to display.

Recursive function is 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

Algorithm

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)
   Print </p><p>
End
STOP

Example

is:

Example

#include <stdio.h>
int 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("</p><p>"); //for new line
}
int main(int argc, char const *argv[]) {
   int n = 7;
   pattern(n);
   return 0;
}

Output

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete