5
45
345
2345
12345
/**
* 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=i; j<=N; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
5
45
345
2345
12345
5 45 345 2345 12345
If you look to the above pattern you will find that it is same as the pattern we just printed above except of trailing spaces. Hence, the whole logic of printing the pattern will be same as first pattern, we only need to add the logic to print spaces. If you hover mouse on to the pattern you can see or count total spaces per row. In the given pattern each row contains i - 1 spaces (where i is the current row number). Note that row are in descending order i.e. row1 is 5, row2 is 4 and so on.
Step-by-step descriptive logic:
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.