program to find the magic square

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

What is linear search?

678


What does do in c?

611


Is file a keyword in c?

503


Explain the use of keyword 'register' with respect to variables.

590


What are qualifiers and modifiers c?

549






How to write a code for implementing my own printf() and scanf().... Please hep me in this... I need a guidance... Can you give an coding for c... Please also explain about the header files used other than #include...

4906


how to print the character with maximum occurence and print that number of occurence too in a string given ?

2036


What are the advantages and disadvantages of a heap?

714


Is it fine to write void main () or main () in c?

548


How do you use a 'Local Block'?

725


What is main function in c?

551


Write a progarm to find the length of string using switch case?

1612


#include main() { char s[] = "Bouquets and Brickbats"; printf(" %c, ",*(&s[2])); printf("%s, ",s+5); printf(" %s",s); printf(" %c",*(s+2)); }

667


Can I use base-2 constants (something like 0b101010)? Is there a printf format for binary?

579


What does %2f mean in c?

676