Basic Right Triangle Number Pattern

Program to Print Basic Right Triangle Number Pattern

PRINTING PATTERN:

1
23
456
78910
            

PREREQUISITE:

Basic knowledge of C language and loops.

ALGORITHM:

1. Take the number of rows (n) as input from the user. This will determine the height of the triangle.
2. Initialize a variable (let's call it 'num') to 1. This variable will be used to print the numbers in the pattern.
3. Initialize a loop that runs from 1 to n (inclusive) to iterate through each row.
4. For each row (let's call the current row index 'i'), initialize another loop that runs from 1 to 'i' (inclusive) to print the numbers in that row.
5. Inside the inner loop, print the current value of 'num', and then increment 'num' by 1.
6. After finishing the inner loop for a row, print a newline character to move to the next row.
7. Repeat steps 3 to 6 until all rows are printed.

Code in C:

#include <stdio.h>

int main() {
    int n, i, j, num = 1;  // Declaring integers for rows, loop counters, and the number to print
    printf("Enter the number of rows for the right triangle number pattern:\n");
    scanf("%d", &n);  // Taking input for number of rows

    // Loop to print the right triangle number pattern
    for (i = 1; i <= n; i++) {  // Loop for each row
        for (j = 1; j <= i; j++) {  // Loop for each column in the current row
            printf("%d", num);  // Printing the current number
            num++;  // Incrementing the number
        }
        printf("\n");  // New line after each row
    }

    return 0;
}
            

Output:

Enter the number of rows for the right triangle number pattern:
4
1
23
456
78910