Learn about Program for Printing Pattern using Loops in C in the below code example. Also refer the comments in the code snippet to get a detailed view about what’s actually happening.
Contents
Program for Printing Pattern using Loops in C
Source code:
#include<stdio.h>
int main()
{
int n;
printf("Enter n value: ");
scanf("%d",&n);
int len = 2*n-1;
for(int row=0; row<len; row++)
{
for(int col=0; col<len; col++)
{
int min = row<col? row:col;
min=min<len-row? min:len-row-1;
min=min<len-col? min:len-col-1;
printf("%d ",n-min);
}
printf("\n");
}
return 0;
}
Output:
Enter n value: 5 5 5 5 5 5 5 5 5 5 5 4 4 4 4 4 4 4 5 5 4 3 3 3 3 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 2 1 2 3 4 5 5 4 3 2 2 2 3 4 5 5 4 3 3 3 3 3 4 5 5 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5
Hope above code works for you and Refer the below Related Codes to gain more insights. Happy coding and come back again.
Similar Codes :
Binary to Octal using Function
C Program to convert Binary to Octal