C program to print magic square of order n where n > 3 and n
is odd

Answers were Sorted based on User's Feedback



C program to print magic square of order n where n > 3 and n is odd..

Answer / aparna v

#include<stdio.h>
#include<conio.h>
void main()
{
int a[20][20],r,c,br,bc,k,n;
clrscr();
printf("Enter the Order of Magic Square(Odd): ");
scanf("%d",&n);
for(r=0;r<n;r++)
for(c=0;c<n;c++)
a[r][c]=0;
r=0;
c=n/2;
for(k=1;k<=n*n;k++)
{
a[r][c]=k;
br=r++;bc=c++;
r=(r+1)%n;
c=(c+1)%n;
if(a[r][c]!=0)
{
c=bc;
r=br-1;
if(r<0)r=n-1;
}
}
printf("The Magic Square...\n");
for(r=0;r<n;r++)
{
for(c=0;c<n;c++)
printf("%4d",a[r][c]);
printf("\n");
}
}

Read more:
http://www.funonthenet.in/forums/index.php?topic=25587.0#ixzz1YUKtxDVT

Is This Answer Correct ?    19 Yes 5 No

C program to print magic square of order n where n > 3 and n is odd..

Answer / puri

just write r=br+1; in place of r=br-1;

Is This Answer Correct ?    9 Yes 9 No

Post New Answer

More C Code Interview Questions

Give a oneline C expression to test whether a number is a power of 2?

25 Answers   EA Electronic Arts, Google, Motorola,


write a program in c language to get the value of arroy keys pressed and display the message which arrow key is pressed?

1 Answers  


main() { struct student { char name[30]; struct date dob; }stud; struct date { int day,month,year; }; scanf("%s%d%d%d", stud.rollno, &student.dob.day, &student.dob.month, &student.dob.year); }

1 Answers  


Write a routine to implement the polymarker function

0 Answers   TCS,


All the combinations of prime numbers whose sum gives 32

1 Answers   HHH,






Give a one-line C expression to test whether a number is a power of 2.

10 Answers   Microsoft,


# include <stdio.h> int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

1 Answers  


main() { int k=1; printf("%d==1 is ""%s",k,k==1?"TRUE":"FALSE"); }

1 Answers  


Write a complete program that consists of a function that can receive two numbers from a user (M and N) as a parameter. Then print all the numbers between the two numbers including the number itself. If the value of M is smaller than N, print the numbers in ascending flow. If the value of M is bigger than N, print the numbers in descending flow. may i know how the coding look like?

2 Answers  


typedef struct error{int warning, error, exception;}error; main() { error g1; g1.error =1; printf("%d",g1.error); }

1 Answers  


main() { int *j; { int i=10; j=&i; } printf("%d",*j); }

9 Answers   HCL, Wipro,


how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


Categories