12345 23451 34521 45321 54321
/**
* C program to print number pattern
* www.atnyla.com
*/
#include <stdio.h>
int main()
{
int rows, cols, i, j;
/* Input rows and columns from user */
printf("Enter number of rows: ");
scanf("%d", &rows);
printf("Enter number of columns: ");
scanf("%d", &cols);
for(i=1; i<=rows; i++)
{
for(j=i; j<=cols; j++)
{
printf("%d", j);
}
for(j=i-1; j>=1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
Enter number of rows: 5
Enter number of columns: 5
12345
23451
34521
45321
54321
Basic C programming, Loop
Before you get into this pattern it is highly recommended that you must learn some basic of number pattern printing.
Once you get acquainted with basics number pattern printing, take a minute and have a close eye on this pattern. If you can notice you might divide this entire pattern in two patterns. Let me show.
12345 2345- 345-- 45--- 5----
----- ----1 ---21 --321 -4321
After looking on both patterns separately I assume that you can easily decode the logic of these two patterns and can combine in single logic. If not here's what you need to do.
And you are done. Lets implement this now...
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.