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 understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.
Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.