Search C Program | nimishsoft@gmail.com

Perfect Square

1   2   3   4   5   6   7   8   9   10
36  37  38  39  40  41  42  43  44  11
35  64  65  66  67  68  69  70  45  12
34  63  84  85  86  87  88  71  46  13
33  62  83  96  97  98  89  72  47  14
32  61  82  95  100 99  90  73  48  15
31  60  81  94  93  92  91  74  49  16
30  59  80  79  78  77  76  75  50  17
29  58  57  56  55  54  53  52  51  18
28  27  26  25  24  23  22  21  20  19


#include<stdio.h> 
int main()
{
    int a[10][10]={0},i,j,low=0,top=9,n=1;
    for(i=0;i<5;i++,low++,top--)
    {
        for(j=low;j<=top;j++,n++)
            a[i][j]=n;
        for(j=low+1;j<=top;j++,n++)
            a[j][top]=n;
        for(j=top-1;j>=low;j--,n++)
            a[top][j]=n;
        for(j=top-1;j>low;j--,n++)
            a[j][low]=n;
    }
    printf("\t\t\t\tPerfect Square\n");
    for(i=0;i<10;i++)
    {
        printf("\n\n\t");
        for(j=0;j<10;j++)
        {
            printf("%6d",a[i][j]);
            delay(300);
        }
    }
    return 0;
}


Related Links:
- More Number Pattern Programs
- Star Pattern Programs in C
- Alphabet Pattern Programs in C
- Series Programs in C

22 comments:

  1. In function `main':
    undefined reference to `delay'

    given code is not working ......

    ReplyDelete
  2. How will u do it for any n*n matrix any generalized algorithm for doing it ?

    ReplyDelete
    Replies
    1. just take input in top variable

      Delete
    2. GIVE 'n' VALUE
      #include
      #define MAX 100
      int main()
      {
      int a[MAX][MAX],i,j,low=0,top,n=1,t;
      printf("enter the size of spiral matrix");
      scanf("%d",&t);
      top=t-1;
      for(i=0;i<=(t/2);i++,low++,top--)
      {
      for(j=low;j<=top;j++,n++)
      a[i][j]=n;

      for(j=low+1;j<=top;j++,n++)
      a[j][top]=n;

      for(j=top-1;j>=low;j--,n++)
      a[top][j]=n;

      for(j=top-1;j>low;j--,n++)
      a[j][low]=n;
      }
      for(i=0;i<t;i++)
      {
      printf("\n\n\t");
      for(j=0;j<t;j++)
      printf("%6d",a[i][j]);
      }
      }


      Delete
  3. can I have this program with the help of loops?

    ReplyDelete
  4. can i hav this prog for 16 elents only

    ReplyDelete
  5. J is not initialised before loops

    ReplyDelete
  6. can i get the dry run please

    ReplyDelete
  7. can someone please explain the algorithm of this program?

    ReplyDelete
  8. #include

    int main()
    {
    int ps[10][10]={0},i=0,j=0,k=0,l=0,down=0,top=0,left=0,right=1;
    while(l<100)
    {
    if(right&&j<10&&ps[i][j]==0)
    {
    ps[i][j]=++l;
    printf("right %d\n",l);
    ++j;
    if(j>=10||ps[i][j]!=0)
    {
    --j;
    ++i;
    right=0;
    down=1;
    top=0;
    left=0;
    }
    }
    else if(down&&i<10&&ps[i][j]==0)
    {
    ps[i][j]=++l;
    printf("down %d\n",l);
    ++i;
    if(i>=10||ps[i][j]!=0)
    {
    --i;
    --j;
    right=0;
    down=0;
    top=0;
    left=1;
    }
    }
    else if(left&&j>=0&&ps[i][j]==0)
    {
    ps[i][j]=++l;
    printf("left %d\n",l);
    --j;
    if(j<0||ps[i][j]!=0)
    {
    ++j;
    --i;
    right=0;
    down=0;
    left=0;
    top=1;
    }
    }
    else if(top&&i>=0&&ps[i][j]==0)
    {
    ps[i][j]=++l;
    printf("top %d\n",l);
    --i;
    if(i<0||ps[i][j]!=0)
    {
    ++i;
    ++j;
    right=1;
    down=0;
    left=0;
    top=0;
    }
    }

    }
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    printf("%4d ",ps[i][j]);
    }
    printf("\n");
    }
    }

    ReplyDelete
  9. //java code using recursion
    package differ;

    public class box {
    private static int i,j,n=1;
    private static int left=0,right=9,up=0,down=9;
    private static int[][] a = new int[10][10];
    public static void main(String[] x)
    {
    printbox(left,right,up,down);
    }
    public static void printbox(int l,int r,int u,int d)
    {
    for(i=l;i<=r;i++)
    {
    a[u][i] = n++;
    if(n>=101)finalprint();
    }
    for(i=u+1;i<=d;i++)
    {
    a[i][r] = n++;
    if(n>=101)finalprint();
    }
    for(i=r-1;i>=l;i--)
    {
    a[d][i] = n++;
    if(n>=101)finalprint();
    }
    for(i=d-1;i>u;i--)
    {
    a[i][l] = n++;
    if(n>=101)finalprint();
    }
    l++;r--;u++;d--;
    printbox(l,r,u,d);
    }
    public static void finalprint()
    {
    for(i=0;i<10;i++)
    {
    for(j=0;j<10;j++)
    {
    System.out.print(a[i][j]+ "\t");
    }System.out.println();
    }
    System.exit(0);
    }
    }

    ReplyDelete
  10. #include
    void change(int &mks,int& cr,int& cc)
    {
    if(cr==(11-mks)&&cc(11-mks)) cc--;
    else if(cc==(11-mks)&&cr>(12-mks)) cr--;
    else if(cc==(11-mks)&&cr==(12-mks)){
    mks--;
    cc++;
    }
    }
    int main()
    {
    int r,c;
    for(r=1;r<=10;r++)
    {
    for(c=1;c<=10;c++)
    {
    int cr=1,cc=1,mks=10,n=1;
    while(!(cr==r&&cc==c))
    {
    change(mks,cr,cc);
    n++;
    }
    if(n<=12)
    printf("%d ",n);
    else
    if(n<=99)
    printf("%d ",n);
    else
    printf("%d ",n);
    n++;
    }
    printf("\n");
    }
    return 0;
    }

    ReplyDelete
  11. #include
    int main()
    {
    int a[10][10]={0},i,j,low=0,top=9,n=1;
    for(i=0;i<5;i++,low++,top--)
    {
    for(j=low;j<=top;j++,n++)
    a[i][j]=n;
    for(j=low+1;j<=top;j++,n++)
    a[j][top]=n;
    for(j=top-1;j>=low;j--,n++)
    a[top][j]=n;
    for(j=top-1;j>low;j--,n++)
    a[j][low]=n;
    }
    printf("\t\t\t\tPerfect Square\n");
    for(i=0;i<10;i++)
    {
    printf("\n\n\t");
    for(j=0;j<10;j++)
    {
    printf("%6d",a[i][j]);
    delay(300);
    }
    }
    return 0;
    }





    this is not run by this know

    ReplyDelete