Print Magic Square in C
In this article, you will learn about the magic square with its example and explanation.
A magic square in C is like a puzzle board made up of rows and columns of numbers. What's special about it is that you'll always get the same total if you add up the numbers in any row, any column, or even the numbers along the main diagonal (like a slanting line from the top-left to the bottom-right).
These special square arrangements have fascinated mathematicians and artists for ages because they have a certain elegance and uniqueness. They're not just for fun, though; they find use in math games, secret codes, and other interesting things.
Simply put, a magic square is like a puzzle with numbers that have a special trick. They always give you the same answer when you add them up in certain ways. People have been creating these puzzles for a long time, and they're not just cool to look at, but they also have practical uses in various areas.
Example:
Let's take a C program for creating a magic square when an order is given.
#include <stdio.h>
void generateOddMagicSquare(int n) {
int magicSquare[n][n];
int num = 1;
for (int i = 0; i< n; i++) {
for (int j = 0; j < n; j++) {
magicSquare[i][j] = 0;
}
}
int row = 0;
int col = n / 2;
while (num<= n * n) {
magicSquare[row][col] = num;
int newRow = (row - 1 + n) % n;
int newCol = (col + 1) % n;
if (magicSquare[newRow][newCol] != 0) {
row = (row + 1) % n;
} else {
row = newRow;
col = newCol;
}
num++;
}
printf("Magic Square of Order %d:\n", n);
for (int i = 0; i< n; i++) {
for (int j = 0; j < n; j++) {
printf("%4d", magicSquare[i][j]);
}
printf("\n");
}
}
void generateEvenMagicSquare(int n) {
int magicSquare[n][n];
int i, j;
for (i = 0; i< n; i++) {
for (j = 0; j < n; j++) {
magicSquare[i][j] = 0;
}
}
i = 0;
j = n / 2;
for (int num = 1; num<= n * n; num++) {
magicSquare[i][j] = num;
i--;
j++;
if (num % n == 0) {
i += 2;
j--;
} else {
if (j == n) {
j -= n;
} else if (i< 0) {
i += n;
}
}
}
printf("Magic Square of Order %d:\n", n);
for (i = 0; i< n; i++) {
for (j = 0; j < n; j++) {
printf("%4d", magicSquare[i][j]);
}
printf("\n");
}
}
int main() {
int order;
printf("Enter the order of the magic square: ");
scanf("%d", &order);
if (order % 2 == 1) {
generateOddMagicSquare(order);
} else if (order % 4 == 0) {
generateEvenMagicSquare(order);
} else {
printf("Please enter an odd order or an even order that is a multiple of 4.\n");
}
return 0;
}
Output:
![Print Magic Square in C](https://static.tutorialandexample.com/c/print-magic-square-in-c1.png)
![Print Magic Square in C](https://static.tutorialandexample.com/c/print-magic-square-in-c2.png)
Explanation:
Odd Magic Squares:
The generateOddMagicSquare function is responsible for creating magic squares with odd orders (like 3x3, 5x5, 7x7, etc.). It uses the Siamese method. Imagine a "cursor" moving diagonally upwards and to the right. At each step, the cursor places a number in the square, starting from the middle of the first row. If the cursor goes outside the square, it wraps around to the opposite side. If the square is already occupied, the cursor moves one step down. This process continues until all cells are filled, creating the magic square.
Even Magic Squares:
The generateEvenMagicSquare function creates magic squares with even orders that are multiples of 4 (like 4x4, 8x8, etc.). It uses the Strachey method. This approach starts by placing numbers in a spiral pattern from the top-left corner. If a cell is occupied, it moves one step to the right and two steps upwards. When a number is a multiple of the order, it moves one step down instead. This results in an even magic square.
Main Function:
The main function is the program's entry point. It asks you to input the order of the magic square you want. If the order is odd, it calls the generateOddMagicSquare function. If the order is a multiple of 4, it calls the generateEvenMagicSquare function. Otherwise, it gives an error message indicating that you should enter either an odd order or an even order that's a multiple of 4.
Printing the Magic Square:
Both generateOddMagicSquare and generateEvenMagicSquare functions build the magic square and print it out using nested loops. The rows and columns of the magic square are displayed, and each number is nicely aligned using the printf() function.