Basic Incrementing Triangle Pattern
Understanding the Problem
The goal is to print a triangle pattern where numbers increment sequentially, and each row has an increasing number of elements.
Pattern Explanation
The pattern follows a triangular structure where numbers increase sequentially row by row.
1
2*3
4*5*6
7*8*9*10
Observations:
- The pattern consists of 4 rows.
- Numbers increment from left to right.
- Each row contains one more element than the previous row.
- Each number is followed by a '*', except the last number in each row.
Algorithm
The pattern follows these steps:
- Initialize a number counter starting from 1.
- Loop through rows (from 1 to N).
- Within each row, loop through columns (up to the row number).
- Print the number followed by '*' except for the last number in the row.
- Increment the number after each print.
- Move to the next line after printing each row.
Method 1: Using Nested Loops
This method uses two loops to print the pattern.
#include <stdio.h>
void printPattern(int n) {
int num = 1;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("%d", num++);
if (j < i) printf("*");
}
printf("\n");
}
}
int main() {
int size = 4;
printPattern(size);
return 0;
}
Output:
1
2*3
4*5*6
7*8*9*10