program to find the magic square



program to find the magic square..

Answer / balaji ganesh

#include <stdio.h>
#include <math.h>
#include <stdlib.h>
int main()
{
int S[15][15],z=1,m,n,i,j,b=1,C,X,Y,T,p=0;
clrscr();
scanf("%d",&n,printf("enter size of magic square you
want:"));
while(z)
{
for (i=0; i<n; i++)
for (j=0; j<n; j++)

S[i][j]=-1;
X=0;
Y=(n/2);

C=b;
for (i=0; i<(n*n); i++)
{
S[X][Y]=C++;
if (--X<0) X=n-1;
if (--Y<0) Y=n-1;
if (S[X][Y] !=-1)
{
for (j=0; j<2; j++)
if (++X > (n-1))
X=0;

if (++Y > (n-1)) Y=0;
}
}
T=(((n*n*n)+n)/2)+p;
printf("\nThe Follwing Magic Square Adds Up
To: %d\n\n", T);
for(i=0; i<=(n-1); i++)
{
for(j=0; j<=(n-1); j++)
printf("%d\t", S[i][j]);
printf("\n\n");
}
printf("\n");
scanf("%d",&z,printf("if u want another
magic square for same size enter any num (to exit 0):"));
b++;p+=n;
}
getch();
}


Is This Answer Correct ?    5 Yes 11 No

Post New Answer

More C Interview Questions

Explain which function in c can be used to append a string to another string?

0 Answers  


what is the difference between #include<stdio.h> and #include"stdio.h" ?

6 Answers   TCS,


Explain how can I prevent another program from modifying part of a file that I am modifying?

0 Answers  


void main(int argc,char *argv[],char *env[]) { int i; for(i=1;i<argc;i++) printf("%s",env[i]); }

3 Answers  


code for selection sort?

1 Answers  






Difference between fopen() and open()?

3 Answers   Aricent,


study the code: #include<stdio.h> void main() { const int a=100; int *p; p=&a; (*p)++; printf("a=%dn(*p)=%dn",a,*p); } What is printed? A)100,101 B)100,100 C)101,101 D)None of the above

15 Answers   Accenture, TCS,


C program to find all possible outcomes of a dice?

0 Answers  


write a program fibonacci series and palindrome program in c

0 Answers   Aditi Placement Service,


what is the output on the screen? int n; n=printf("my name is %d",printf("kiran %d",printf("kumar"))); printf("\n %d \n",n);

4 Answers   TCS,


what is memory leak?

3 Answers  


int main() { int i=-1,j=-1;k=0,l=2,m; m=i++&&j++&&k++||l++; printf("%d%d%d%d%d",i,j,k,l,m); }

3 Answers   HCL,


Categories