Pattern 1: Right-Aligned Number Triangle
1
12
123
1234
12345
Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print numbers
for (int j = 1; j <= i; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Pattern 2: Full Pyramid of Numbers
1
123
12345
1234567
123456789
Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print numbers
for (int j = 1; j <= 2 * i - 1; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
Pattern 3: Inverted Pyramid of Numbers
123456789
1234567
12345
123
1
Code:
#include <stdio.h>
int main() {
int rows = 5;
for (int i = rows; i >= 1; i--) {
// Print spaces
for (int j = 1; j <= rows - i; j++) {
printf(" ");
}
// Print numbers
for (int j = 1; j <= 2 * i - 1; j++) {
printf("%d", j);
}
printf("\n");
}
return 0;
}
How It Works:
- Spaces: Use an inner loop to print spaces to align the numbers properly.
- Numbers: Another inner loop prints numbers in a sequence for each row.
- Outer Loop: Determines the number of rows.
Comments
Post a Comment