program to find magic aquare using array

Answers were Sorted based on User's Feedback



program to find magic aquare using array..

Answer / aditya raj

// magic square for odd numbers....

#include<stdio.h>
main()
{
int n;
scanf("%d",&n);
int a[n][n],i,j;
int row[n],col[n],dia[2]={0};
for(i=0;i<n;i++)
{
row[i]=0;
col[i]=0;
}
int num=1;
int nn=n*3/2;
for(i=0; i < n; i++)
for(j=0; j < n; j++)

a[(j-i+nn)%n][(i*2-j+n)%n]=num++;

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

}

printf("\n");
}
}

Is This Answer Correct ?    4 Yes 1 No

program to find magic aquare using array..

Answer / anurag

This is the link to read something about the magic aquare.
try it.
htp://mathworld.wolfram.com/MagicSquare.html

Is This Answer Correct ?    3 Yes 1 No

program to find magic aquare using array..

Answer / aditya raj

// magic square for odd numbers....

#include<stdio.h>
main()
{
int n;
scanf("%d",&n);
int a[n][n],i,j;
int row[n],col[n],dia[2]={0};
for(i=0;i<n;i++)
{
row[i]=0;
col[i]=0;
}
int num=1;
int nn=n*3/2;
for(i=0; i < n; i++)
for(j=0; j < n; j++)

a[(j-i+nn)%n][(i*2-j+n)%n]=num++;

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

}

printf("\n");
}
}

Is This Answer Correct ?    0 Yes 1 No

program to find magic aquare using array..

Answer / radhika

Nine

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More C Code Interview Questions

#include<stdio.h> main() { const int i=4; float j; j = ++i; printf("%d %f", i,++j); }

1 Answers  


Is it possible to type a name in command line without ant quotes?

1 Answers   Excel, Infosys,


String reverse with time complexity of n/2 with out using temporary variable.

10 Answers   NetApp, Symantec,


#include<stdio.h> #include<conio.h> void main() { int a=(1,2,3,(1,2,3,4); switch(a) { printf("ans:"); case 1: printf("1");break; case 2: printf("2");break; case 3: printf("1");break; case 4: printf("4");break; printf("end"); } getch(); }

0 Answers  


program to find the roots of a quadratic equation

14 Answers   College School Exams Tests, Engineering, HP, IIIT, Infosys, Rajiv Gandhi University of Knowledge Technologies RGUKT, SSC,






main() { int *ptr=(int*)malloc(sizeof(int)); *ptr=4; printf("%d",(*ptr)+++*ptr++); }

3 Answers   GNITC,


main() { while (strcmp(“some”,”some\0”)) printf(“Strings are not equal\n”); }

1 Answers  


Is it possible to print a name without using commas, double quotes,semi-colons?

7 Answers  


#define prod(a,b) a*b main() { int x=3,y=4; printf("%d",prod(x+2,y-1)); }

1 Answers  


What is the main difference between STRUCTURE and UNION?

13 Answers   HCL,


source code for delete data in array for c

1 Answers   TCS,


Sorting entire link list using selection sort and insertion sort and calculating their time complexity

1 Answers   Infosys, Microsoft, NetApp,


Categories