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

#define max 5 #define int arr1[max] main() { typedef char arr2[max]; arr1 list={0,1,2,3,4}; arr2 name="name"; printf("%d %s",list[0],name); }

1 Answers  


main( ) { char *q; int j; for (j=0; j<3; j++) scanf(“%s” ,(q+j)); for (j=0; j<3; j++) printf(“%c” ,*(q+j)); for (j=0; j<3; j++) printf(“%s” ,(q+j)); }

1 Answers  


Question: We would like to design and implement a programming solution to the reader-writer problem using semaphores in C language under UNIX. We assume that we have three readers and two writers processes that would run concurrently. A writer is to update (write) into one memory location (let’s say a variable of type integer named temp initialized to 0). In the other hand, a reader is to read the content of temp and display its content on the screen in a formatted output. One writer can access the shared data exclusively without the presence of other writer or any reader, whereas, a reader may access the shared memory for reading with the presence of other readers (but not writers).

1 Answers  


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

1 Answers   HCL,


main() { printf("%x",-1<<4); }

3 Answers   HCL, Sokrati, Zoho,






All the combinations of prime numbers whose sum gives 32

1 Answers   HHH,


write a program to find out roots of quadratic equation "x=-b+-(b^2-4ac0^-1/2/2a"

2 Answers  


why do you use macros? Explain a situation where you had to incorporate macros in your proc report? use a simple instream data example with code ?

0 Answers  


main() { int i; printf("%d",scanf("%d",&i)); // value 10 is given as input here }

2 Answers   IBM,


In the following pgm add a stmt in the function fun such that the address of 'a' gets stored in 'j'. main(){ int * j; void fun(int **); fun(&j); } void fun(int **k) { int a =0; /* add a stmt here*/ }

1 Answers  


How do you create a really large matrix (i.e. 3500x3500) in C without having the program crash? I can only reach up to 2500. It must have something to do with lack of memory. Please help!

1 Answers  


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

2 Answers   Accenture,


Categories