여기에서 주어진 문제 패턴에 따라 재귀 접근 방식을 사용하여 표시해야 합니다.
재귀 함수 n 번 자신을 호출하는 것입니다. 프로그램에는 'n'개의 재귀 함수가 있을 수 있습니다. 재귀 함수로 작업하는 문제는 복잡성입니다.
알고리즘
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 \n End STOP
예
#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("\n"); //for new line
}
int main(int argc, char const *argv[]) {
int n = 7;
pattern(n);
return 0;
} 출력
위의 프로그램을 실행하면 다음과 같은 출력이 생성됩니다.
* ** *** **** ***** ****** *******