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

Answer Posted / 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       View All Answers


Please Help Members By Posting Answers For Below Questions

Cluster head selection in Wireless Sensor Network using C programming language.

3133


Write a Program in 'C' To Insert a Unique Number Only. (Hint: Just Like a Primary Key Numbers In Database.) Please Some One Suggest Me a Better Solution for This question ??

1791


How do you verify if the two sentences/phrases input is an anagram using predefined functions in string.h and by using arrays?

2034


What is data _null_? ,Explain with code when u need to use it in data step programming ?

2834


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.

3336






why nlogn is the lower limit of any sort algorithm?

2392


create a C-code that will display the total fare of a passenger of a taxi if the driver press enter,the timer will stop. Every 10 counts is 2 pesos. Initial value is 25.00

6327


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

3724


Given a spherical surface, write bump-mapping procedure to generate the bumpy surface of an orange

2874


Write a program to model an exploding firecracker in the xy plane using a particle system

3705


How can you relate the function with the structure? Explain with an appropriate example.

2930


Hi, i have a project that the teacher want a pyramid of numbers in C# or java...when we click a button...the pyramid should be generated in a listbox/or JtextArea...and the pyramid should have the folowing form: 1 232 34543 4567654 567898765 67890109876 7890123210987 890123454321098 90123456765432109 0123456789876543210 Plz help with codes...didn't find anything on the net.

2700


To Write a C program to remove the repeated characters in the entered expression or in entered characters(i.e) removing duplicates. String contains only lowercase characters ['a'-'z']

514


write a simple calculator c program to perform addition, subtraction, mul and div.

3166


how to test pierrot divisor

2274