Write a C program to print the given triangular number pattern using for loop. How to print the given number pattern using for loop in C programming. Logic to print the given number pattern using C program.
Required knowledge
Logic to print the given pattern
The given pattern is very much similar to one of previous explained pattern with little trick. To get the logic of this pattern take a moment and think. If still its difficult to get the logic. Read the below observations about the pattern.
- It contains total N rows (where N is the total number of rows to be printed).
- Each row contains total i columns (where i is the current row number).
- For each row less than N / 2 current row number i.e. i gets printed.
- For each row greater than N / 2 a value N – i + 1 gets printed.
Lets, write down code for the given pattern.
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++)
{
for(j=1; j<=i; j++)
{
if(i <= (N/2))
{
printf("%d", i);
}
else
{
printf("%d", (N - i + 1));
}
}
printf("\n");
}
return 0;
}
Output
Enter rows: 5 1 22 333 2222 11111