Back to Problem list
Analysis:
When i = 0, j = 0, count++, count = 1, print 1, print a new line.. printf("\n");
When i = 1,
j = 0, count++, count = 2, print 2
j = 1, count++, count = 3, print 3
print a new line.. printf("\n");
When i = 2, j = 0, 1, 2, we print 456 ( similar way)
.
.
.
When i = 4......print 1112131415
Back to Problem list
Problem 19: Write a program to print the following pyramid:
1
23
456
78910
1112131415
Solution:
#include<stdio.h>
void Print() {
int i, j, count = 0;
for(i = 0; i<5; i++) { // this loop is used to print each line/ row,
for(j = 0; j<= i; j++) { // this loop is used to print each column
count++;
printf("%d",count);
}
printf("\n"); // printing new line after each row
}
}
int main() {
Print();
return 0;
}
|
Analysis:
When i = 0, j = 0, count++, count = 1, print 1, print a new line.. printf("\n");
When i = 1,
j = 0, count++, count = 2, print 2
j = 1, count++, count = 3, print 3
print a new line.. printf("\n");
When i = 2, j = 0, 1, 2, we print 456 ( similar way)
.
.
.
When i = 4......print 1112131415
Back to Problem list