5
54
543
5432
54321
/**
* 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=1; j<i; j++)
{
printf(" ");
}
// Logic to print numbers
for(j=N; j>=i; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
5
54
543
5432
54321
5 54 543 5432 54321
If you are done with first pattern, then logic to this wouldn't be much difficult to get. This pattern is almost similar to the previous pattern we just printed, except trailing spaces before the number. Hence, logic to print the pattern will be same as the first pattern, we only need to add the logic of printing spaces. You can hover your mouse cursor to the pattern to see or count the number of space. The pattern consists of i - 1 spaces per row (where i is the current row number). Note that in the given pattern we have assumed that row numbers are ordered descending from N-1.
Step-by-step descriptive logic to print spaces:
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.