Concentric Pattern Program in C

Concentric Pattern Program in C

How to Write Concentric pattern program in C ? or How to Write Square pattern Program in C language.



#include <stdio.h>
#include<conio.h>
void main()
{
int  i,j,n,m;
int a[50][50];
printf("enter the number of side of square:");
scanf("%d",&n);
m=2*n-1;
for(i=0;i<m;i++)
{
  for(j=i;j<=m-i-1;j++)
  {
 
    a[i][j]=n;
  }
  for(j=i+1;j<=m-i-1;j++)
  {
 
    a[j][m-i-1]=n;
  }
  for(j=m-2-i;j>=i;j--)
  {
 
    a[m-i-1][j]=n;
 
  }
  for(j=m-2-i;j>i;j--)
  {
    a[j][i]=n;
  }
  n--;
}

printf("print the pattern\n");
for(i=0;i<m;i++)
{

  for(j=0;j<m;j++)
  {
    printf("\t%d",a[i][j]);
  }
  printf("\n");

}
getch();
}

Output:-



Concentric pattern program with another logic

#include <stdio.h>
#include<conio.h>
void main()
{
  int i,j,n,a,b;
  printf("enter number of side");
  scanf("%d",&n);
  a=n*2-1;
  for(i=0;i<a;i++)
  {
    for(j=0;j<a;j++)
    {
   
       b=i<j?i:j;
      b=b<a-i?b:a-i-1;
      b=b<a-j-1?b:a-j-1;
      printf("\t%d",n-b);     
    }
    printf("\n");
  }
getch();
}

Output:-
     

No comments:

Post a Comment