55555
4444
333
22
1
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter N: ");
scanf("%d", &N);
for(i=N; i>=1; i--)
{
// Logic to print spaces
for(j=N; j>i; j--)
{
printf(" ");
}
// Logic to print numbers
for(j=1; j<=i; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
55555
4444
333
22
1
Now, once you got the logic of previous number pattern you can easily get the logic of this pattern. As is it same as pattern 1 just we need to add trailing spaces before the number gets printed. If you point your mouse over the below pattern you can count the number of spaces per row and can easily get the logic in which spaces are printed in the pattern.
55555
4444
333
22
1
Here the spaces are in ascending order i.e. row1 contains 0 spaces, row2 contains 1, row3 contains 2 and so on. Also each row contains current_row_number - 1 spaces. Logic to print spaces inside outer loop is:
Lets, now implement this on 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.