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

main() { printf("%d, %d", sizeof('c'), sizeof(100)); } a. 2, 2 b. 2, 100 c. 4, 100 d. 4, 4

18 Answers   HCL, IBM, Infosys, LG Soft, Satyam,


main( ) { int a[2][3][2] = {{{2,4},{7,8},{3,4}},{{2,2},{2,3},{3,4}}}; printf(“%u %u %u %d \n”,a,*a,**a,***a); printf(“%u %u %u %d \n”,a+1,*a+1,**a+1,***a+1); }

2 Answers  


main() { int i =10, j = 20; clrscr(); printf("%d, %d, ", j-- , --i); printf("%d, %d ", j++ , ++i); } a. 20, 10, 20, 10 b. 20, 9, 20, 10 c. 20, 9, 19, 10 d. 19, 9, 20, 10

4 Answers   HCL,


main() { int i=5; printf("%d",++i++); }

1 Answers  


how to create a 3x3 two dimensional array that will give you the sums on the left and bottom columns

0 Answers  






4. Main() { Int i=3,j=2,c=0,m; m=i&&j||c&I; printf(“%d%d%d%d”,I,j,c,m); }

2 Answers   Broadridge,


source code for delete data in array for c

1 Answers   TCS,


void main() { char ch; for(ch=0;ch<=127;ch++) printf(“%c %d \n“, ch, ch); }

1 Answers  


Given a list of numbers ( fixed list) Now given any other list, how can you efficiently find out if there is any element in the second list that is an element of the first list (fixed list)

3 Answers   Disney, Google, ZS Associates,


# include<stdio.h> aaa() { printf("hi"); } bbb(){ printf("hello"); } ccc(){ printf("bye"); } main() { int (*ptr[3])(); ptr[0]=aaa; ptr[1]=bbb; ptr[2]=ccc; ptr[2](); }

1 Answers  


How to swap two variables, without using third variable ?

104 Answers   AB, ADP, BirlaSoft, Cisco, Cygnet Infotech, HCL, Hewitt, Honeywell, HP, IBM, Infosys, Manhattan, Microsoft, Mobius, Percept, Satyam, SofTMware, TCS, Wipro, Yamaha,


main() { static int a[3][3]={1,2,3,4,5,6,7,8,9}; int i,j; static *p[]={a,a+1,a+2}; for(i=0;i<3;i++) { for(j=0;j<3;j++) printf("%d\t%d\t%d\t%d\n",*(*(p+i)+j), *(*(j+p)+i),*(*(i+p)+j),*(*(p+j)+i)); } }

1 Answers  


Categories