C Program to print Nested Square Pattern using numbers for n number of lines.[Pattern 16]| Using C language| C Program series 37|
👨💻C Program to print Nested Square Pattern (▣) using numbers for n number of lines.[Pattern:16]
Code :
For n number of LINES:
/*
C Program to print Nested Square Pattern using numbers for n number of lines.[Pattern 16]
For Example:n=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
*/
#include <stdio.h>
int main()
{
printf("C Program to print Nested Square Pattern using numbers for n number of lines.[Pattern 16]\n\n");
int n;
printf("Enter the number: ");
scanf("%d", &n);
printf("Nested Squar Pattern.\n",n);
int i , j , start=0 , len=(2*n)-1 , end=len-1 , a[len][len] ;//here we used to 2D-array to solve this.
for( ;n!=0;start++,end--,n--)
{
for(i=start;i<=end;i++)
{
for(j=start;j<=end;j++)
{
if(i==start||i==end||j==start||j==end)
{
a[i][j]=n;
}
}
}
}
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
printf("%d ",a[i][j]);
}
printf("\n");
}
return 0;
}
The output of Code:
Comments
Post a Comment