Create and print a magic phalanx of 1 to n^2 natural numbers

Source: Internet
Author: User

#include "stdio.h"
void Main (void)
{
int a[15][15]={0};
int i,j,k,n,p=1;

while (p==1)//parameter p is set to determine N input range
{
printf ("Please enter an odd n (1 to 15):");
scanf ("%d", &n);
if ((n>0) && (n<=15) && (n%2!=0))//The Order of the magic square should be odd, even for the time being not oh ~ ~ Hehe
p = =;
}

Build the Magic Phalanx.
J=N/2;
A[0][j]=1; Put "1" in the middle of the first row.
i=0;
J=N/2;
for (k=2;k<=n*n;k++)
{
I=i-1;
j=j+1;
if (i<0)
i=n-1;
if (j> (n-1))
j=0;
A[i][j]=k;
if (k%n==0)//When the number inserted is an integer multiple of n, the next starting point is set to the next line in his column
{
A[++i][j]=++k;
}
}

printf ("The magic Square:/n");
for (i=0;i<n;i++)
{
for (j=0;j<n;j++)
{
printf ("%4d", A[i][j]);
}
printf ("n");
}
printf ("n");


}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.