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

Print the given pattern recursively

王林
Release: 2023-09-17 10:13:06
forward
1344 people have browsed it

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
Copy after login

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;
}
Copy after login

Output

If we run the above program, it will generate The following output.

*
**
***
****
*****
******
*******
Copy after login

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!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template