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

int i; main(){ int t; for ( t=4;scanf("%d",&i)-t;printf("%d\n",i)) printf("%d--",t--); } // If the inputs are 0,1,2,3 find the o/p

2 Answers  


What is the difference between proc means and proc tabulate ? explain with a simple example when you have to use means or tabulate?

1 Answers  


#include<stdio.h> main() { FILE *ptr; char i; ptr=fopen("zzz.c","r"); while((i=fgetch(ptr))!=EOF) printf("%c",i); }

1 Answers  


main() { int i=3; switch(i) { default:printf("zero"); case 1: printf("one"); break; case 2:printf("two"); break; case 3: printf("three"); break; } }

1 Answers  


#define clrscr() 100 main() { clrscr(); printf("%d\n",clrscr()); }

2 Answers  






union u { struct st { int i : 4; int j : 4; int k : 4; int l; }st; int i; }u; main() { u.i = 100; printf("%d, %d, %d",u.i, u.st.i, u.st.l); } a. 4, 4, 0 b. 0, 0, 0 c. 100, 4, 0 d. 40, 4, 0

1 Answers   HCL,


main() { int i=10; i=!i>14; Printf ("i=%d",i); }

1 Answers  


/*what is the output for*/ void main() { int r; printf("Naveen"); r=printf(); getch(); }

4 Answers  


#define f(g,g2) g##g2 main() { int var12=100; printf("%d",f(var,12)); }

3 Answers  


PROG. TO PRODUCE 1 1 1 1 2 1 1 3 3 1 1 4 6 4 1

1 Answers  


Write a C program to print look and say sequence? For example if u get the input as 1 then the sequence is 11 21 1211 111221 312211 12112221 .......(it counts the no. of 1s,2s etc which is in successive order) and this sequence is used in run-length encoding.

1 Answers  


void main() { int i=i++,j=j++,k=k++; printf(ā€œ%d%d%dā€,i,j,k); }

1 Answers  


Categories