CrazyEngineers
  • (using c)Generating Magic Squares of odd order using De la Loubre's method

    Updated: Oct 27, 2024
    Views: 906
    Magic squares are nothing but where row,column and diagonal sums are constant
    for e.g enter odd number say 9 then you will get the output as magic square as follows
    Read the method here
    #-Link-Snipped-#

    2ng4vid
    #include<stdio.h>
    #include<dos.h>
    void main()
    {
    int a[100][100],x,y,tmp=1,n;
    clrscr();
    printf("\n Enter the odd no for which you want to generate magic square\n");
    scanf("%d",&n);
    for(x=0;x<=n-1;x++)
    {
    for(y=0;y<=n-1;y++)
    {
    a[x][y]=0;
    }
    }
    x=0;
    y=(n-1)/2;
    
    if(!(n%2==0))
    {
    a[x][y]=tmp;
    hello:
    x-=1;
    y+=1;
    if(tmp>=n*n)
    goto raw;
    if((x<0)&&(y>=n))
    {
    x+=2;
    y-=1;
    goto another;
    }
    if(x<0)
    x=n-1;
    if(y>=n)
    y=0;
    another:
    tmp+=1;
    if(a[x][y]==0)
    {
    a[x][y]=tmp;
    }
    else
    {
    y-=1;
    x+=2;
    a[x][y]=tmp;
    }
    goto hello;
    
    
    }
    else
    {
    printf("\n you have not entered an odd number\n");
    }
    raw:
    printf("\n");
    for(x=0;x<=n-1;x++)
    {
    for(y=0;y<=n-1;y++)
    {
    delay(300);
    printf("%d\t",a[x][y]);
    }
    printf("\n");
    }
    getch();
    }
    
    0
    Replies
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Home Channels Search Login Register