Write a C program to print the given number pattern using loop. How to print the given number pattern using loop in C programming. Logic to print the given number pattern using C program.
Required knowledge
Logic to print the given pattern
On first look the pattern may seem little difficult to print out. Lets make things little easier and dissect the given pattern in two internal parts.
Between these two patterns spaces are printed in decreasing order i.e first row contains 8, second row contains 6 spaces and so on last row contains 0 spaces. Loop formation to print spaces will be for(j=i*2; j<N*2; j++).
Loop to print the above two pattern will be similar and simple. It has also been discussed in one of my earlier post. Lets combine everything in single program.
Program to print the given pattern
/**
* C program to print the given number pattern
*/
#include <stdio.h>
int main()
{
int i, j, N;
printf("Enter rows: ");
scanf("%d", &N);
for(i=1; i<=N; i++)
{
// Prints first part of pattern
for(j=1; j<=i; j++)
{
printf("%d", j);
}
// Prints spaces between two parts
for(j=i*2; j<N*2; j++)
{
printf(" ");
}
// Prints second part of the pattern
for(j=i; j>=1; j--)
{
printf("%d", j);
}
printf("\n");
}
return 0;
}
Output
Enter rows: 5 1 1 12 21 123 321 1234 4321 1234554321
Happy coding 😉