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

#include<stdio.h> main() { struct xx { int x; struct yy { char s; struct xx *p; }; struct yy *q; }; }

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  


#define DIM( array, type) sizeof(array)/sizeof(type) main() { int arr[10]; printf(“The dimension of the array is %d”, DIM(arr, int)); }

1 Answers  


Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution)

7 Answers   Microsoft,


what will be the position of the file marker? a: fseek(ptr,0,SEEK_SET); b: fseek(ptr,0,SEEK_CUR);

2 Answers  






4. Main() { Int i=3,j=2,c=0,m; m=i&&j||c&I; printf(“%d%d%d%d”,I,j,c,m); }

2 Answers   Broadridge,


What is the hidden bug with the following statement? assert(val++ != 0);

1 Answers  


main() { int i = 3; for (;i++=0;) printf(“%d”,i); }

1 Answers   CSC,


main() { char s[ ]="man"; int i; for(i=0;s[ i ];i++) printf("\n%c%c%c%c",s[ i ],*(s+i),*(i+s),i[s]); }

1 Answers   DCE,


int i=10; main() { extern int i; { int i=20; { const volatile unsigned i=30; printf("%d",i); } printf("%d",i); } printf("%d",i); }

1 Answers  


main() { int i=4,j=7; j = j || i++ && printf("YOU CAN"); printf("%d %d", i, j); }

1 Answers  


3) Int Matrix of certain size was given, We had few valu= es in it like this. =97=97=97=97=97=97=97=97=97=97=97 1 = | 4 | | 5 | &= nbsp; | 45 =97=97=97=97=97=97=97=97=97=97=97 &n= bsp; | 3 | 3 | 5 | = | 4 =97=97=97=97=97=97=97=97=97=97=97 34 |&nbs= p; 3 | 3 | | 12 | &= nbsp; =97=97=97=97=97=97=97=97=97=97=97 3 | &nbs= p; | 3 | 4 | = | 3 =97=97=97=97=97=97=97=97=97=97=97 3 | = ; | | | = ; 3 | =97=97=97=97=97=97=97=97=97=97=97 &= nbsp; | | 4 | = ; | 4 | 3 We w= ere supposed to move back all the spaces in it at the end. Note: = If implemented this prog using recursion, would get higher preference.

0 Answers   RoboSoft,


Categories