#include #include int f(int n, int x, int y) { return (x + y * 2 + 1) % n; } void generateMagicSquare(int n) { int i, j; if (n < 3 || (n % 2) == 0) { printf("Please enter a valid odd number greater than or equal to 3 for the order of the magic square.\n"); return; } for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("% 4d", f(n, n - j - 1, i) * n + f(n, j, i) + 1); putchar('\n'); } printf("\n Magic Constant: %d.\n", (n * n + 1) / 2 * n); } void siameseMethod(int n) { int magicSquare[n][n]; // Initialize all elements of the magic square to 0 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { magicSquare[i][j] = 0; } } int num = 1; int row = 0, col = n / 2; while (num <= n * n) { magicSquare[row][col] = num; int next_row = (row - 1 + n) % n; int next_col = (col + 1) % n; if (magicSquare[next_row][next_col] != 0) { row = (row + 1) % n; } else { row = next_row; col = next_col; } num++; } // Printing the magic square printf("Magic Square of order %d:\n", n); for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { printf("%3d ", magicSquare[i][j]); } printf("\n"); } printf("\n Magic Constant: %d.\n", (n * n + 1) / 2 * n); } // A function to generate odd sized magic squares void generateSquare(int n) { int magicSquare[n][n]; // set all as 0 memset(magicSquare, 0, sizeof(magicSquare)); // Initialize position for 1 int i = n / 2; int j = n - 1; // One by one put all values in magic square for (int num = 1; num <= n * n;) { if (i == -1 && j == n) // 3rd condition { j = n - 2; i = 0; } else { // 1st condition helper if next number // goes to out of square's right side if (j == n) j = 0; // 1st condition helper if next number // is goes to out of square's upper side if (i < 0) i = n - 1; } if (magicSquare[i][j]) // 2nd condition { j -= 2; i++; continue; } else magicSquare[i][j] = num++; // set number j++; i--; // 1st condition } // Print magic square printf("The Magic Square for n=%d:\nSum of " "each row or column %d:\n\n", n, n * (n * n + 1) / 2); for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%3d ", magicSquare[i][j]); printf("\n"); } } void generateOddMagicSquare(int n) { int magicSquare[n][n]; int num = 1; int row = 0, col = n / 2; //fill the square, alternatively use memset for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { magicSquare[i][j] = 0; } } while (num <= n * n) { magicSquare[row][col] = num; int next_row = (row - 1 + n) % n; int next_col = (col + 1) % n; if (num % n == 0) { row = (row + 1) % n; } else { row = next_row; col = next_col; } 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"); } printf("\nMagic Constant: %d\n", (n * n + 1) / 2 * n); } int main() { int n; do { printf("Enter an odd number (n >= 3) for the order of the magic square: "); scanf("%d", &n); if (n < 3 || (n % 2) == 0) printf("Please enter an odd number greater than or equal to 3 for the order of the magic square.\n"); } while (n < 3 || (n % 2) == 0); //version one (uses some extra function, study it) generateMagicSquare(n); printf("\n\n"); //version two (plain 2d matrix use) siameseMethod(n); printf("\n\n"); // version three (very similar) // https://www.geeksforgeeks.org/magic-square/ generateSquare(n); printf("\n\n"); // version four (plain from wikipedia description) generateOddMagicSquare(n); return 0; }