write a c program to print magic square of order n when n>3
and n is odd?



write a c program to print magic square of order n when n>3 and n is odd?..

Answer / v p p

#include<stdio.h>
#include<conio.h>
void main()
{
int i,j=1,k,n,a[22][22],p,q,r,count=0;
clrscr();
A:
printf("Enter No. Of Rows Or Columns(Should Be Odd) : ");
scanf("%d",&n);
printf("\n");
if(n%2==0)
{
printf("Enter Odd No.s Only\n\n");
goto A;
}
k=(n+1)/2;
for(p=1;p<=n;p++)
{ for(q=1;q<=n;q++)
{
a[p][q]=NULL;
}
}
for(i=1;i<=(n*n);i++)
{
if(a[j][k]==NULL)
{
a[j][k]=i;
}
else
{
j=j+2;
k--;
if(j==n+2)
j=2;
if(k==0)
k=n;
if(j==0)
j=n;
if(k==n+1)
k=1;
a[j][k]=i;
}
j--;
k++;
if(j==0)
j=n;
if(k==n+1)
k=1;
}
for(p=1;p<=n;p++)
{
for(q=1;q<=n;q++)
{
r=a[p][q];
while(r>0)
{
r=r/10;
count++;
}
if(count==1)
printf(" 0%d",a[p][q]);
else
printf(" %d",a[p][q]);
if(q%n==0)
printf("\n\n");
count=0;
}
}
for(p=1;p<=n;p++)
{
for(q=1;q<=n;q++)
{
}
}
getch();
}

Is This Answer Correct ?    18 Yes 6 No

Post New Answer

More C Code Interview Questions

How to count a sum, when the numbers are read from stdin and stored into a structure?

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,


void main() { int const * p=5; printf("%d",++(*p)); }

3 Answers   Infosys, Made Easy, State Bank Of India SBI,


Is the following code legal? void main() { typedef struct a aType; aType someVariable; struct a { int x; aType *b; }; }

1 Answers  


How to access command-line arguments?

4 Answers  






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

7 Answers  


how can i search an element in an array

2 Answers   CTS, Microsoft, ViPrak,


Write a program to receive an integer and find it's octal equivalent. How can i do with using while loop.

2 Answers  


const int perplexed = 2; #define perplexed 3 main() { #ifdef perplexed #undef perplexed #define perplexed 4 #endif printf("%d",perplexed); } a. 0 b. 2 c. 4 d. none of the above

1 Answers   emc2, HCL,


main() { while (strcmp(“some”,”some\0”)) printf(“Strings are not equal\n”); }

1 Answers  


Program to find the largest sum of contiguous integers in the array. O(n)

11 Answers  


main() { int i = 258; int *iPtr = &i; printf("%d %d", *((char*)iPtr), *((char*)iPtr+1) ); }

1 Answers  


Categories