How to Recursively Print the Pattern 1 n 2 3 n 4 5 6 n 7 8 9 10 in C

How to Recursively Print the Pattern 1 n 2 3 n 4 5 6 n 7 8 9 10 in C

This article explores the process of recursively printing the given pattern in C programming. We will walk through the logic and implementation of two different recursive functions that can achieve this pattern.

Introduction to Recursive Pattern Printing in C

Recursive functions are a powerful tool in C programming, especially when dealing with problems that can be broken down into smaller, similar subproblems. One such problem is printing a specific pattern where each line contains an incrementing sequence of numbers, followed by a newline, and the pattern is repeated for each line up to a certain limit.

First Approach: Recursive Function with Three Parameters

The first approach involves a recursive function with three parameters: the num (the current number to be printed), line (the current line number), and max (the maximum line number).

Define the function void pattern(int num, int line, int max) to handle the recursive logic. Use an if-else statement to check if the current line exceeds the maximum line number. If it does, return from the function. Otherwise, initialize a loop to print the numbers for the current line. The loop will print numbers from num to line. After printing the numbers, call the pattern function again with the next line number (incremented by 1) and the same max value. In the main function, initialize the line number and call the pattern function with the appropriate arguments.

Example Code for First Approach

include stdio.h
void pattern(int num, int line, int max) {
    if (line  max) return;
    else {
        for(int i  num; i  (line   1) / 2; i  ) {
            printf("%i ", i);
        }
        printf("
");
        pattern(num   (line   1), line   1, max);
    }
}
int main(void) {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", n);
    pattern(1, 1, n);
    return 0;
}

Second Approach: Recursive Function with Two Parameters

A more condensed version of the solution can be achieved by using only two parameters: the current line number and the maximum line number. In this version, the function needs to calculate the starting number for each line based on the current line number.

Define the function void pattern(int line, int max) for the recursive logic. Follow the same if-else logic to check if the current line exceeds the maximum line number. If it does, return from the function. Otherwise, initialize a loop to print the numbers for the current line. The loop will print numbers from 1 to (line / 2). After printing the numbers, call the pattern function again with the next line number (incremented by 1) and the same max value. In the main function, initialize the line number and call the pattern function with the appropriate arguments.

Example Code for Second Approach

include stdio.h
void pattern(int line, int max) {
    if (line  max) return;
    else {
        for(int i  1; i  (line / 2); i  ) {
            printf("%i ", i);
        }
        printf("
");
        pattern(line   1, max);
    }
}
int main(void) {
    int n;
    printf("Enter the number of lines: ");
    scanf("%d", n);
    pattern(1, n);
    return 0;
}

Conclusion

Both approaches discussed here demonstrate how to use recursion to print the required pattern in C. The first approach provides more clarity by separating the variables, while the second approach is more concise and uses a single variable to determine the starting number for each line.

Using recursive functions effectively requires understanding the base case (when to stop the recursion) and the recursive case (how to break down the problem).

Feel free to ask any questions or explore further with different variations of the pattern and other recursive problems.