11111 2222 333 44 5
/**
* C program to print number pattern
* www.atnyla.com
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Logic to print numbers
for(j=i; j<=N; j++)
{
printf("%d", i);
}
printf("\n");
}
return 0;
}
Enter N: 5
11111
2222
333
44
5
Basic C Programming, Loop
11111 2222 333 44 5
To get the logic of above pattern just have a close eye on the pattern. Lets suppose that the rows starts from 1 to N (where N is the total rows to be printed). Then total columns per row is N - current_row_number + 1 i.e. first row contains 5 - 1 + 1 = 5 columns and so on. And for each column the current row number gets printed. Hence, the step by step descriptive logic to print the given logic is:
And you are done, 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.