11111
2222
333
44
5
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Logic to print spaces
for(j=1; j<i; j++)
{
printf(" ");
}
// Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
11111
2222
333
44
5
11111
2222
333
44
5
Now, once you get the logic of printing the previous number pattern you can easily print this pattern. As both are similar except that it contains extra trailing spaces. Hence you only need to add the logic of printing spaces before the number gets printed in the first number pattern. If you point your mouse over the pattern you can actually count total number of spaces per row and can get the logic in which spaces are printed. Now as you can see, each row contains row_number - 1 spaces. Logic to print spaces is-
Lets now write down its code.
First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.