What is Magic Square:- Square which contains further divided smaller N x N squares.Each square contains a number such that sum of each row ,column and diagonal is same,like if size of magic square is 3 x 3 then sum of each row,column and diagonal is 15and if 5 x 5 then value is 65.Sum of row,column or diagonal of magic square can be easily calculated by using the formula n(n^2 + 1)/2 . Now coming towards the implementation of magic square program in c for odd size
Step 1: Initialize the N x N array with 0 will be used to check the location is empty or not.
Step 2: make provisions to make the array circular i.e by using %.Place 1 at the middle location of
the 1st row.
Step 3: create the loop for remaining (n*n)-1 numbers to be placed in magic square and place them by following rules:If the location in the upper row next column is empty i.e 0 then place the value over there else place it at the row below the above thus get the magic square for odd size of square.
printf("enter size of magic squre:");
printf("Enter the odd value in the range of 25\n");
p=i-1<0?n-1 : i-1;
printf("\nHere is the magic square\n");
Share your views regarding the given post via the comments section given below.
If you liked the post, please '+1' it & share it on other social networks.