555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555
/**
* C program to print number pattern
*/
#include <stdio.h>
int main()
{
int N, i, j;
printf("Enter N: ");
scanf("%d", &N);
// First upper half of the pattern
for(i=N; i>=1; i--)
{
// First inner part of upper half
for(j=N; j>i; j--)
{
printf("%d", j);
}
// Second inner part of upper half
for(j=1; j<=(i*2-1); j++)
{
printf("%d", i);
}
// Third inner part of upper half
for(j=i+1; j<=N; j++)
{
printf("%d", j);
}
printf("\n");
}
// Second lower half of the pattern
for(i=1; i<N; i++)
{
// First inner part of lower half
for(j=N; j>i; j--)
{
printf("%d", j);
}
// Second inner part of lower half
for(j=1; j<=(i*2-1); j++)
{
printf("%d", i+1);
}
// Third inner part of lower half
for(j=i+1; j<=N; j++)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Enter N: 5
555555555
544444445
543333345
543222345
543212345
543222345
543333345
544444445
555555555
Basic C programming, Loop
Before you get into this confusing but interesting number pattern I recommend you to get yourself acquainted with the basics of number printing, learn the basic logic to print number pattern.
555555555 544444445 543333345 543222345 543212345 543222345 543333345 544444445 555555555
Once you get acquainted with basics of number pattern printing, have a careful eye on to above pattern. Let me help you, to make things easier lets divide this entire pattern into two big parts and six smaller pieces. The first upper half contains three parts
--------- 5-------- 54------- 543------ 5432----- --------- --------- --------- ---------
555555555 -4444444- --33333-- ---222--- ----1---- --------- --------- --------- ---------
--------- --------5 -------45 ------345 -----2345 --------- --------- --------- ---------
And the lower half of the pattern also contains three separate parts.
--------- --------- --------- --------- --------- 5432----- 543------ 54------- 5--------
--------- --------- --------- --------- --------- ----2---- ---333--- --44444-- -5555555-
--------- --------- --------- --------- --------- -----2345 ------345 -------45 --------5
Now, both upper and lower half's of the pattern would be printed separately in two separate outer loops. Considering the first upper half of the pattern. The logic to print upper half of the pattern:
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.