12345 21234 32123 43212 54321
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int N, i, j;
printf("Enter N: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Print first part
for(j=i; j>1; j--)
{
printf("%d", j);
}
// Print second part
for(j=1; j<= (N-i +1); j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Enter N: 5
12345
21234
32123
43212
54321
Basic C programming, Loop
Before learning the logic of this number pattern, you first must be acquainted with some basic number patterns.
Now, once you are acquainted with some basic logic to print number pattern. If you look to both the patterns you will find both similar to each other. Hence, if you get the logic of one you can easily print the second one. Now lets get into first pattern, take a minute and have a close eye to the below pattern.
12345 21234 32123 43212 54321
If you can notice, you can actually divide this in two parts to make things easier. Let me show.
----- 2---- 32--- 432-- 5432-
12345 -1234 --123 ---12 ----1
Now you can find that printing these patterns separately is relatively easier than the whole. Below is the logic to print this pattern as a whole.
And you are done. Lets implement this on code.
Note: I won't be explaining the logic of second pattern as both are similar in-fact second pattern is reverse of first.
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.