Magic Squares of Even Order

Here given code implementation process.

// C Program 
// Magic Squares of Even Order
#include <stdio.h>

// Find magic square of Even Order
void magicSquare(int n)
{
	if (n % 2 == 0)
	{
		int matrix[n][n];
		int k = n / 2;
		int row = 0;
		int col = k / 2;
		int value = 0;
		if ((n / 2) % 2 != 0)
		{
			// When n is Even and its divisible by 2 reminder is not zero
			// 6,10,14,18,22,26..(4*n +2)
			// Exmple (6/2 = 3) and its reminder not zero
			// Execute loop (n/2)*2 times
			for (value = 1; value <= k *k; ++value)
			{
				// Add element in top left grid
				matrix[row][col] = value;
				// Add element in bottom right grid
				matrix[k + row][k + col] = value + (k *k);
				// Add element in top right grid
				matrix[row][k + col] = value + (k *k *2);
				// Add element in bottom left grid
				matrix[k + row][col] = value + (k *k *3);
				col += 1;
				row -= 1;
				if (value % k == 0)
				{
					col -= 1;
					row += 2;
				}
				else
				{
					if (col == k)
					{
						col = 0;
					}
					else if (row < 0)
					{
						row += k;
					}
				}
			}
		}
		else
		{
			// Doubly Even order magic square of n
			// (4*n)
			for (row = 0; row < n; row++)
			{
				for (col = 0; col < n; col++)
				{
					matrix[row][col] = (n *row) + col + 1;
				}
			}
			for (row = 0; row < n / 4; row++)
			{
				for (col = 0; col < n / 4; col++)
				{
					// Top Left corner
					matrix[row][col] = (n *n + 1) - matrix[row][col];
				}
			}
			for (row = 0; row < n / 4; row++)
			{
				for (col = 3 *(n / 4); col < n; col++)
				{
					// Top right corner
					matrix[row][col] = (n *n + 1) - matrix[row][col];
				}
			}
			for (row = 3 *n / 4; row < n; row++)
			{
				for (col = 0; col < n / 4; col++)
				{
					// Bottom Left corner
					matrix[row][col] = (n *n + 1) - matrix[row][col];
				}
			}
			for (row = 3 *n / 4; row < n; row++)
			{
				for (col = 3 *n / 4; col < n; col++)
				{
					// Bottom right corner
					matrix[row][col] = (n *n + 1) - matrix[row][col];
				}
			}
			for (row = n / 4; row < 3 *n / 4; row++)
			{
				for (col = n / 4; col < 3 *n / 4; col++)
				{
					// Centre elements
					matrix[row][col] = (n *n + 1) - matrix[row][col];
				}
			}
		}
		printf("  Magic square of size (%dX%d) \n", n, n);
		// Display result
		for (row = 0; row < n; ++row)
		{
			for (col = 0; col < n; ++col)
			{
				printf("  %d", matrix[row][col]);
			}
			printf("\n");
		}
		printf("  Sum of each rows and columns is %d \n\n", (n *(n *n + 1) / 2));
	}
}
int main(int argc, char const *argv[])
{
	// Test cases
	magicSquare(6);
	magicSquare(10);
	magicSquare(4);
	magicSquare(8);
	return 0;
}

Output

  Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
  Sum of each rows and columns is 111

  Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
  Sum of each rows and columns is 505

  Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
  Sum of each rows and columns is 34

  Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
  Sum of each rows and columns is 260
/*
  Java Program for
  Magic Squares of Even Order
*/
class Square
{
       
    // Find magic square of Even Order
    public void magicSquare(int n)
    {
        if (n % 2 == 0)
        {
            int[][] matrix = new int[n][n];
            int k = n / 2;
            int row = 0;
            int col = k / 2;
            int value = 0;
            if ((n / 2) % 2 != 0)
            {
                // When n is Even and its divisible by 2 reminder is not zero
                // 6,10,14,18,22,26..(4*n +2)
                // Exmple (6/2 = 3) and its reminder not zero
                // Execute loop (n/2)*2 times
                for (value = 1; value <= k * k; ++value)
                {
                    // Add element in top left grid
                    matrix[row][col] = value;
                    // Add element in bottom right grid
                    matrix[k + row][k + col] = value + (k * k);
                    // Add element in top right grid
                    matrix[row][k + col] = value + (k * k * 2);
                    // Add element in bottom left grid
                    matrix[k + row][col] = value + (k * k * 3);
                    col += 1;
                    row -= 1;
                    if (value % k == 0)
                    {
                        col -= 1;
                        row += 2;
                    }
                    else
                    {
                        if (col == k)
                        {
                            col = 0;
                        }
                        else if (row < 0)
                        {
                            row += k;
                        }
                    }
                }
            }
            else
            {
                // Doubly Even order magic square of n
                // (4*n)
                for (row = 0; row < n; row++)
                {
                    for (col = 0; col < n; col++)
                    {
                        matrix[row][col] = (n * row) + col + 1;
                    }
                }
                for (row = 0; row < n / 4; row++)
                {
                    for (col = 0; col < n / 4; col++)
                    {
                        // Top Left corner
                        matrix[row][col] = (n * n + 1) - matrix[row][col];
                    }
                }
                for (row = 0; row < n / 4; row++)
                {
                    for (col = 3 * (n / 4); col < n; col++)
                    {
                        // Top right corner
                        matrix[row][col] = (n * n + 1) - matrix[row][col];
                    }
                }
                for (row = 3 * n / 4; row < n; row++)
                {
                    for (col = 0; col < n / 4; col++)
                    {
                        // Bottom Left corner
                        matrix[row][col] = (n * n + 1) - matrix[row][col];
                    }
                }
                for (row = 3 * n / 4; row < n; row++)
                {
                    for (col = 3 * n / 4; col < n; col++)
                    {
                        // Bottom right corner
                        matrix[row][col] = (n * n + 1) - matrix[row][col];
                    }
                }
                for (row = n / 4; row < 3 * n / 4; row++)
                {
                    for (col = n / 4; col < 3 * n / 4; col++)
                    {
                        // Centre elements
                        matrix[row][col] = (n * n + 1) - matrix[row][col];
                    }
                }
            }
            System.out.print(" Magic square of size (" + n + "X" + n + ") \n");
            // Display result
            for (row = 0; row < n; ++row)
            {
                for (col = 0; col < n; ++col)
                {
                    System.out.print("  " + matrix[row][col] );
                }
                System.out.print("\n");
            }
            System.out.print(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
        }
    }
    public static void main(String[] args)
    {
        Square task = new Square();
        task.magicSquare(6);
        task.magicSquare(10);
        task.magicSquare(4);	
        task.magicSquare(8);
    }

}

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Magic Squares of Even Order
*/
class Square
{
	public:
		// Find magic square of Even Order
		void magicSquare(int n)
		{
			if (n % 2 == 0)
			{
				int matrix[n][n];
				int k = n / 2;
				int row = 0;
				int col = k / 2;
				int value = 0;
				if ((n / 2) % 2 != 0)
				{
					// When n is Even and its divisible by 2 reminder is not zero
					// 6,10,14,18,22,26..(4*n +2)
					// Exmple (6/2 = 3) and its reminder not zero
					// Execute loop (n/2)*2 times
					for (value = 1; value <= k *k; ++value)
					{
						// Add element in top left grid
						matrix[row][col] = value;
						// Add element in bottom right grid
						matrix[k + row][k + col] = value + (k *k);
						// Add element in top right grid
						matrix[row][k + col] = value + (k *k *2);
						// Add element in bottom left grid
						matrix[k + row][col] = value + (k *k *3);
						col += 1;
						row -= 1;
						if (value % k == 0)
						{
							col -= 1;
							row += 2;
						}
						else
						{
							if (col == k)
							{
								col = 0;
							}
							else if (row < 0)
							{
								row += k;
							}
						}
					}
				}
				else
				{
					// Doubly Even order magic square of n
					// (4*n)
					for (row = 0; row < n; row++)
					{
						for (col = 0; col < n; col++)
						{
							matrix[row][col] = (n *row) + col + 1;
						}
					}
					for (row = 0; row < n / 4; row++)
					{
						for (col = 0; col < n / 4; col++)
						{
							// Top Left corner
							matrix[row][col] = (n *n + 1) - matrix[row][col];
						}
					}
					for (row = 0; row < n / 4; row++)
					{
						for (col = 3 *(n / 4); col < n; col++)
						{
							// Top right corner
							matrix[row][col] = (n *n + 1) - matrix[row][col];
						}
					}
					for (row = 3 *n / 4; row < n; row++)
					{
						for (col = 0; col < n / 4; col++)
						{
							// Bottom Left corner
							matrix[row][col] = (n *n + 1) - matrix[row][col];
						}
					}
					for (row = 3 *n / 4; row < n; row++)
					{
						for (col = 3 *n / 4; col < n; col++)
						{
							// Bottom right corner
							matrix[row][col] = (n *n + 1) - matrix[row][col];
						}
					}
					for (row = n / 4; row < 3 *n / 4; row++)
					{
						for (col = n / 4; col < 3 *n / 4; col++)
						{
							// Centre elements
							matrix[row][col] = (n *n + 1) - matrix[row][col];
						}
					}
				}
				cout << " Magic square of size (" << n << "X" << n << ") \n";
				// Display result
				for (row = 0; row < n; ++row)
				{
					for (col = 0; col < n; ++col)
					{
						cout << "  " << matrix[row][col];
					}
					cout << "\n";
				}
				cout << " Sum of each rows and columns is " << (n *(n *n + 1) / 2) << " \n\n";
			}
		}
};
int main()
{
	Square task = Square();
	task.magicSquare(6);
	task.magicSquare(10);
	task.magicSquare(4);
	task.magicSquare(8);
	return 0;
}

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
// Include namespace system
using System;
/*
  C# Program for
  Magic Squares of Even Order
*/
public class Square
{
	// Find magic square of Even Order
	public void magicSquare(int n)
	{
		if (n % 2 == 0)
		{
			int[,] matrix = new int[n,n];
			int k = n / 2;
			int row = 0;
			int col = k / 2;
			int value = 0;
			if ((n / 2) % 2 != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				for (value = 1; value <= k * k; ++value)
				{
					// Add element in top left grid
					matrix[row,col] = value;
					// Add element in bottom right grid
					matrix[k + row,k + col] = value + (k * k);
					// Add element in top right grid
					matrix[row,k + col] = value + (k * k * 2);
					// Add element in bottom left grid
					matrix[k + row,col] = value + (k * k * 3);
					col += 1;
					row -= 1;
					if (value % k == 0)
					{
						col -= 1;
						row += 2;
					}
					else
					{
						if (col == k)
						{
							col = 0;
						}
						else if (row < 0)
						{
							row += k;
						}
					}
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				for (row = 0; row < n; row++)
				{
					for (col = 0; col < n; col++)
					{
						matrix[row,col] = (n * row) + col + 1;
					}
				}
				for (row = 0; row < n / 4; row++)
				{
					for (col = 0; col < n / 4; col++)
					{
						// Top Left corner
						matrix[row,col] = (n * n + 1) - matrix[row,col];
					}
				}
				for (row = 0; row < n / 4; row++)
				{
					for (col = 3 * (n / 4); col < n; col++)
					{
						// Top right corner
						matrix[row,col] = (n * n + 1) - matrix[row,col];
					}
				}
				for (row = 3 * n / 4; row < n; row++)
				{
					for (col = 0; col < n / 4; col++)
					{
						// Bottom Left corner
						matrix[row,col] = (n * n + 1) - matrix[row,col];
					}
				}
				for (row = 3 * n / 4; row < n; row++)
				{
					for (col = 3 * n / 4; col < n; col++)
					{
						// Bottom right corner
						matrix[row,col] = (n * n + 1) - matrix[row,col];
					}
				}
				for (row = n / 4; row < 3 * n / 4; row++)
				{
					for (col = n / 4; col < 3 * n / 4; col++)
					{
						// Centre elements
						matrix[row,col] = (n * n + 1) - matrix[row,col];
					}
				}
			}
			Console.Write(" Magic square of size (" + n + "X" + n + ") \n");
			// Display result
			for (row = 0; row < n; ++row)
			{
				for (col = 0; col < n; ++col)
				{
					Console.Write("  " + matrix[row,col]);
				}
				Console.Write("\n");
			}
			Console.Write(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
		}
	}
	public static void Main(String[] args)
	{
		Square task = new Square();
		task.magicSquare(6);
		task.magicSquare(10);
		task.magicSquare(4);
		task.magicSquare(8);
	}
}

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
<?php
/*
  Php Program for
  Magic Squares of Even Order
*/
class Square
{
	// Find magic square of Even Order
	public	function magicSquare($n)
	{
		if ($n % 2 == 0)
		{
			$matrix = array_fill(0, $n, array_fill(0, $n, 0));
			$k = intval($n / 2);
			$row = 0;
			$col = intval($k / 2);
			$value = 0;
			if ((intval($n / 2)) % 2 != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				for ($value = 1; $value <= $k * $k; ++$value)
				{
					// Add element in top left grid
					$matrix[$row][$col] = $value;
					// Add element in bottom right grid
					$matrix[$k + $row][$k + $col] = $value + ($k * $k);
					// Add element in top right grid
					$matrix[$row][$k + $col] = $value + ($k * $k * 2);
					// Add element in bottom left grid
					$matrix[$k + $row][$col] = $value + ($k * $k * 3);
					$col += 1;
					$row -= 1;
					if ($value % $k == 0)
					{
						$col -= 1;
						$row += 2;
					}
					else
					{
						if ($col == $k)
						{
							$col = 0;
						}
						else if ($row < 0)
						{
							$row += $k;
						}
					}
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				for ($row = 0; $row < $n; $row++)
				{
					for ($col = 0; $col < $n; $col++)
					{
						$matrix[$row][$col] = ($n * $row) + $col + 1;
					}
				}
				for ($row = 0; $row < intval($n / 4); $row++)
				{
					for ($col = 0; $col < intval($n / 4); $col++)
					{
						// Top Left corner
						$matrix[$row][$col] = ($n * $n + 1) - $matrix[$row][$col];
					}
				}
				for ($row = 0; $row < intval($n / 4); $row++)
				{
					for ($col = 3 * (intval($n / 4)); $col < $n; $col++)
					{
						// Top right corner
						$matrix[$row][$col] = ($n * $n + 1) - $matrix[$row][$col];
					}
				}
				for ($row = intval(3 * $n / 4); $row < $n; $row++)
				{
					for ($col = 0; $col < intval($n / 4); $col++)
					{
						// Bottom Left corner
						$matrix[$row][$col] = ($n * $n + 1) - $matrix[$row][$col];
					}
				}
				for ($row = intval(3 * $n / 4); $row < $n; $row++)
				{
					for ($col = intval(3 * $n / 4); $col < $n; $col++)
					{
						// Bottom right corner
						$matrix[$row][$col] = ($n * $n + 1) - $matrix[$row][$col];
					}
				}
				for ($row = intval($n / 4); $row < intval(3 * $n / 4); $row++)
				{
					for ($col = intval($n / 4); $col < intval(3 * $n / 4); $col++)
					{
						// Centre elements
						$matrix[$row][$col] = ($n * $n + 1) - $matrix[$row][$col];
					}
				}
			}
			echo " Magic square of size (". $n ."X". $n .") \n";
			// Display result
			for ($row = 0; $row < $n; ++$row)
			{
				for ($col = 0; $col < $n; ++$col)
				{
					echo "  ". $matrix[$row][$col];
				}
				echo "\n";
			}
			echo " Sum of each rows and columns is ". (intval($n * ($n * $n + 1) / 2)) ." \n\n";
		}
	}
}

function main()
{
	$task = new Square();
	$task->magicSquare(6);
	$task->magicSquare(10);
	$task->magicSquare(4);
	$task->magicSquare(8);
}
main();

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
/*
  Node Js Program for
  Magic Squares of Even Order
*/
class Square
{
	// Find magic square of Even Order
	magicSquare(n)
	{
		if (n % 2 == 0)
		{
			var matrix = Array(n).fill(0).map(() => new Array(n).fill(0));
			var k = parseInt(n / 2);
			var row = 0;
			var col = parseInt(k / 2);
			var value = 0;
			if ((parseInt(n / 2)) % 2 != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				for (value = 1; value <= k * k; ++value)
				{
					// Add element in top left grid
					matrix[row][col] = value;
					// Add element in bottom right grid
					matrix[k + row][k + col] = value + (k * k);
					// Add element in top right grid
					matrix[row][k + col] = value + (k * k * 2);
					// Add element in bottom left grid
					matrix[k + row][col] = value + (k * k * 3);
					col += 1;
					row -= 1;
					if (value % k == 0)
					{
						col -= 1;
						row += 2;
					}
					else
					{
						if (col == k)
						{
							col = 0;
						}
						else if (row < 0)
						{
							row += k;
						}
					}
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				for (row = 0; row < n; row++)
				{
					for (col = 0; col < n; col++)
					{
						matrix[row][col] = (n * row) + col + 1;
					}
				}
				for (row = 0; row < parseInt(n / 4); row++)
				{
					for (col = 0; col < parseInt(n / 4); col++)
					{
						// Top Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
					}
				}
				for (row = 0; row < parseInt(n / 4); row++)
				{
					for (col = 3 * (parseInt(n / 4)); col < n; col++)
					{
						// Top right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
					}
				}
				for (row = parseInt(3 * n / 4); row < n; row++)
				{
					for (col = 0; col < parseInt(n / 4); col++)
					{
						// Bottom Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
					}
				}
				for (row = parseInt(3 * n / 4); row < n; row++)
				{
					for (col = parseInt(3 * n / 4); col < n; col++)
					{
						// Bottom right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
					}
				}
				for (row = parseInt(n / 4); row < parseInt(3 * n / 4); row++)
				{
					for (col = parseInt(n / 4); col < parseInt(3 * n / 4); col++)
					{
						// Centre elements
						matrix[row][col] = (n * n + 1) - matrix[row][col];
					}
				}
			}
			process.stdout.write(" Magic square of size (" + n + "X" + n + ") \n");
			// Display result
			for (row = 0; row < n; ++row)
			{
				for (col = 0; col < n; ++col)
				{
					process.stdout.write("  " + matrix[row][col]);
				}
				process.stdout.write("\n");
			}
			process.stdout.write(" Sum of each rows and columns is " + (parseInt(n * (n * n + 1) / 2)) + " \n\n");
		}
	}
}

function main()
{
	var task = new Square();
	task.magicSquare(6);
	task.magicSquare(10);
	task.magicSquare(4);
	task.magicSquare(8);
}
main();

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
#   Python 3 Program for
#   Magic Squares of Even Order

class Square :
	#  Find magic square of Even Order
	def magicSquare(self, n) :
		if (n % 2 == 0) :
			matrix = [[0] * (n) for _ in range(n) ]
			k = int(n / 2)
			row = 0
			col = int(k / 2)
			value = 0
			if ((int(n / 2)) % 2 != 0) :
				#  When n is Even and its divisible by 2 reminder is not zero
				#  6,10,14,18,22,26..(4*n +2)
				#  Exmple (6/2 = 3) and its reminder not zero
				#  Execute loop (n/2)*2 times
				value = 1
				while (value <= k * k) :
					#  Add element in top left grid
					matrix[row][col] = value
					#  Add element in bottom right grid
					matrix[k + row][k + col] = value + (k * k)
					#  Add element in top right grid
					matrix[row][k + col] = value + (k * k * 2)
					#  Add element in bottom left grid
					matrix[k + row][col] = value + (k * k * 3)
					col += 1
					row -= 1
					if (value % k == 0) :
						col -= 1
						row += 2
					else :
						if (col == k) :
							col = 0
						
						elif(row < 0) :
							row += k
						
					
					value += 1
				
			else :
				#  Doubly Even order magic square of n
				#  (4*n)
				row = 0
				while (row < n) :
					col = 0
					while (col < n) :
						matrix[row][col] = (n * row) + col + 1
						col += 1
					
					row += 1
				
				row = 0
				while (row < int(n / 4)) :
					col = 0
					while (col < int(n / 4)) :
						#  Top Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					
					row += 1
				
				row = 0
				while (row < int(n / 4)) :
					col = 3 * (int(n / 4))
					while (col < n) :
						#  Top right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					
					row += 1
				
				row = int(3 * n / 4)
				while (row < n) :
					col = 0
					while (col < int(n / 4)) :
						#  Bottom Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					
					row += 1
				
				row = int(3 * n / 4)
				while (row < n) :
					col = int(3 * n / 4)
					while (col < n) :
						#  Bottom right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					
					row += 1
				
				row = int(n / 4)
				while (row < int(3 * n / 4)) :
					col = int(n / 4)
					while (col < int(3 * n / 4)) :
						#  Centre elements
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					
					row += 1
				
			
			print(" Magic square of size (", n ,"X", n ,") ")
			#  Display result
			row = 0
			while (row < n) :
				col = 0
				while (col < n) :
					print(" ", matrix[row][col], end = "")
					col += 1
				
				print(end = "\n")
				row += 1
			
			print(" Sum of each rows and columns is ", (int(n * (n * n + 1) / 2)) ," \n")
		
	

def main() :
	task = Square()
	task.magicSquare(6)
	task.magicSquare(10)
	task.magicSquare(4)
	task.magicSquare(8)

if __name__ == "__main__": main()

Output

 Magic square of size ( 6 X 6 )
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is  111

 Magic square of size ( 10 X 10 )
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is  505

 Magic square of size ( 4 X 4 )
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is  34

 Magic square of size ( 8 X 8 )
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is  260
#   Ruby Program for
#   Magic Squares of Even Order

class Square 
	#  Find magic square of Even Order
	def magicSquare(n) 
		if (n % 2 == 0) 
			matrix = Array.new(n) {Array.new(n) {0}}
			k = n / 2
			row = 0
			col = k / 2
			value = 0
			if ((n / 2) % 2 != 0) 
				#  When n is Even and its divisible by 2 reminder is not zero
				#  6,10,14,18,22,26..(4*n +2)
				#  Exmple (6/2 = 3) and its reminder not zero
				#  Execute loop (n/2)*2 times
				value = 1
				while (value <= k * k) 
					#  Add element in top left grid
					matrix[row][col] = value
					#  Add element in bottom right grid
					matrix[k + row][k + col] = value + (k * k)
					#  Add element in top right grid
					matrix[row][k + col] = value + (k * k * 2)
					#  Add element in bottom left grid
					matrix[k + row][col] = value + (k * k * 3)
					col += 1
					row -= 1
					if (value % k == 0) 
						col -= 1
						row += 2
					else 
						if (col == k) 
							col = 0
						elsif(row < 0) 
							row += k
						end

					end

					value += 1
				end

			else 
				#  Doubly Even order magic square of n
				#  (4*n)
				row = 0
				while (row < n) 
					col = 0
					while (col < n) 
						matrix[row][col] = (n * row) + col + 1
						col += 1
					end

					row += 1
				end

				row = 0
				while (row < n / 4) 
					col = 0
					while (col < n / 4) 
						#  Top Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					end

					row += 1
				end

				row = 0
				while (row < n / 4) 
					col = 3 * (n / 4)
					while (col < n) 
						#  Top right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					end

					row += 1
				end

				row = 3 * n / 4
				while (row < n) 
					col = 0
					while (col < n / 4) 
						#  Bottom Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					end

					row += 1
				end

				row = 3 * n / 4
				while (row < n) 
					col = 3 * n / 4
					while (col < n) 
						#  Bottom right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					end

					row += 1
				end

				row = n / 4
				while (row < 3 * n / 4) 
					col = n / 4
					while (col < 3 * n / 4) 
						#  Centre elements
						matrix[row][col] = (n * n + 1) - matrix[row][col]
						col += 1
					end

					row += 1
				end

			end

			print(" Magic square of size (", n ,"X", n ,") \n")
			#  Display result
			row = 0
			while (row < n) 
				col = 0
				while (col < n) 
					print("  ", matrix[row][col])
					col += 1
				end

				print("\n")
				row += 1
			end

			print(" Sum of each rows and columns is ", (n * (n * n + 1) / 2) ," \n\n")
		end

	end

end

def main() 
	task = Square.new()
	task.magicSquare(6)
	task.magicSquare(10)
	task.magicSquare(4)
	task.magicSquare(8)
end

main()

Output

 Magic square of size (6X6) 
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111 

 Magic square of size (10X10) 
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505 

 Magic square of size (4X4) 
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34 

 Magic square of size (8X8) 
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260 

/*
  Scala Program for
  Magic Squares of Even Order
*/
class Square
{
	// Find magic square of Even Order
	def magicSquare(n: Int): Unit = {
		if (n % 2 == 0)
		{
			var matrix: Array[Array[Int]] = Array.fill[Int](n, n)(0);
			var k: Int = (n / 2).toInt;
			var row: Int = 0;
			var col: Int = (k / 2).toInt;
			var value: Int = 0;
			if (((n / 2).toInt) % 2 != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				value = 1;
				while (value <= k * k)
				{
					// Add element in top left grid
					matrix(row)(col) = value;
					// Add element in bottom right grid
					matrix(k + row)(k + col) = value + (k * k);
					// Add element in top right grid
					matrix(row)(k + col) = value + (k * k * 2);
					// Add element in bottom left grid
					matrix(k + row)(col) = value + (k * k * 3);
					col += 1;
					row -= 1;
					if (value % k == 0)
					{
						col -= 1;
						row += 2;
					}
					else
					{
						if (col == k)
						{
							col = 0;
						}
						else if (row < 0)
						{
							row += k;
						}
					}
					value += 1;
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				row = 0;
				while (row < n)
				{
					col = 0;
					while (col < n)
					{
						matrix(row)(col) = (n * row) + col + 1;
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < (n / 4).toInt)
				{
					col = 0;
					while (col < (n / 4).toInt)
					{
						// Top Left corner
						matrix(row)(col) = (n * n + 1) - matrix(row)(col);
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < (n / 4).toInt)
				{
					col = 3 * ((n / 4).toInt);
					while (col < n)
					{
						// Top right corner
						matrix(row)(col) = (n * n + 1) - matrix(row)(col);
						col += 1;
					}
					row += 1;
				}
				row = (3 * n / 4).toInt;
				while (row < n)
				{
					col = 0;
					while (col < (n / 4).toInt)
					{
						// Bottom Left corner
						matrix(row)(col) = (n * n + 1) - matrix(row)(col);
						col += 1;
					}
					row += 1;
				}
				row = (3 * n / 4).toInt;
				while (row < n)
				{
					col = (3 * n / 4).toInt;
					while (col < n)
					{
						// Bottom right corner
						matrix(row)(col) = (n * n + 1) - matrix(row)(col);
						col += 1;
					}
					row += 1;
				}
				row = (n / 4).toInt;
				while (row < (3 * n / 4).toInt)
				{
					col = (n / 4).toInt;
					while (col < (3 * n / 4).toInt)
					{
						// Centre elements
						matrix(row)(col) = (n * n + 1) - matrix(row)(col);
						col += 1;
					}
					row += 1;
				}
			}
			print(" Magic square of size (" + n + "X" + n + ") \n");
			// Display result
			row = 0;
			while (row < n)
			{
				col = 0;
				while (col < n)
				{
					print("  " + matrix(row)(col));
					col += 1;
				}
				print("\n");
				row += 1;
			}
			print(" Sum of each rows and columns is " + ((n * (n * n + 1) / 2).toInt) + " \n\n");
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Square = new Square();
		task.magicSquare(6);
		task.magicSquare(10);
		task.magicSquare(4);
		task.magicSquare(8);
	}
}

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260
/*
  Swift 4 Program for
  Magic Squares of Even Order
*/
class Square
{
	// Find magic square of Even Order
	func magicSquare(_ n: Int)
	{
		if (n % 2 == 0)
		{
			var matrix: [
				[Int]
			] = Array(repeating: Array(repeating: 0, count: n), count: n);
			let k: Int = n / 2;
			var row: Int = 0;
			var col: Int = k / 2;
			var value: Int = 0;
			if ((n / 2) % 2  != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				value = 1;
				while (value <= k * k)
				{
					// Add element in top left grid
					matrix[row][col] = value;
					// Add element in bottom right grid
					matrix[k + row][k + col] = value + (k * k);
					// Add element in top right grid
					matrix[row][k + col] = value + (k * k * 2);
					// Add element in bottom left grid
					matrix[k + row][col] = value + (k * k * 3);
					col += 1;
					row -= 1;
					if (value % k == 0)
					{
						col -= 1;
						row += 2;
					}
					else
					{
						if (col == k)
						{
							col = 0;
						}
						else if (row < 0)
						{
							row += k;
						}
					}
					value += 1;
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				row = 0;
				while (row < n)
				{
					col = 0;
					while (col < n)
					{
						matrix[row][col] = (n * row) + col + 1;
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < n / 4)
				{
					col = 0;
					while (col < n / 4)
					{
						// Top Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < n / 4)
				{
					col = 3 * (n / 4);
					while (col < n)
					{
						// Top right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 3 * n / 4;
				while (row < n)
				{
					col = 0;
					while (col < n / 4)
					{
						// Bottom Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 3 * n / 4;
				while (row < n)
				{
					col = 3 * n / 4;
					while (col < n)
					{
						// Bottom right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = n / 4;
				while (row < 3 * n / 4)
				{
					col = n / 4;
					while (col < 3 * n / 4)
					{
						// Centre elements
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
			}
			print(" Magic square of size (", n ,"X", n ,") ");
			// Display result
			row = 0;
			while (row < n)
			{
				col = 0;
				while (col < n)
				{
					print("  ", matrix[row][col], terminator: "");
					col += 1;
				}
				print(terminator: "\n");
				row += 1;
			}
			print(" Sum of each rows and columns is ", (n * (n * n + 1) / 2) ," \n");
		}
	}
}
func main()
{
	let task: Square = Square();
	task.magicSquare(6);
	task.magicSquare(10);
	task.magicSquare(4);
	task.magicSquare(8);
}
main();

Output

 Magic square of size ( 6 X 6 )
   8   1   6   26   19   24
   3   5   7   21   23   25
   4   9   2   22   27   20
   35   28   33   17   10   15
   30   32   34   12   14   16
   31   36   29   13   18   11
 Sum of each rows and columns is  111

 Magic square of size ( 10 X 10 )
   17   24   1   8   15   67   74   51   58   65
   23   5   7   14   16   73   55   57   64   66
   4   6   13   20   22   54   56   63   70   72
   10   12   19   21   3   60   62   69   71   53
   11   18   25   2   9   61   68   75   52   59
   92   99   76   83   90   42   49   26   33   40
   98   80   82   89   91   48   30   32   39   41
   79   81   88   95   97   29   31   38   45   47
   85   87   94   96   78   35   37   44   46   28
   86   93   100   77   84   36   43   50   27   34
 Sum of each rows and columns is  505

 Magic square of size ( 4 X 4 )
   16   2   3   13
   5   11   10   8
   9   7   6   12
   4   14   15   1
 Sum of each rows and columns is  34

 Magic square of size ( 8 X 8 )
   64   63   3   4   5   6   58   57
   56   55   11   12   13   14   50   49
   17   18   46   45   44   43   23   24
   25   26   38   37   36   35   31   32
   33   34   30   29   28   27   39   40
   41   42   22   21   20   19   47   48
   16   15   51   52   53   54   10   9
   8   7   59   60   61   62   2   1
 Sum of each rows and columns is  260
/*
  Kotlin Program for
  Magic Squares of Even Order
*/
class Square
{
	// Find magic square of Even Order
	fun magicSquare(n: Int): Unit
	{
		if (n % 2 == 0)
		{
			var matrix: Array <Array<Int>> = Array(n){Array(n){0}};
			var k: Int = n / 2;
			var row: Int = 0;
			var col: Int = k / 2;
			var value: Int ;
			if ((n / 2) % 2 != 0)
			{
				// When n is Even and its divisible by 2 reminder is not zero
				// 6,10,14,18,22,26..(4*n +2)
				// Exmple (6/2 = 3) and its reminder not zero
				// Execute loop (n/2)*2 times
				value = 1;
				while (value <= k * k)
				{
					// Add element in top left grid
					matrix[row][col] = value;
					// Add element in bottom right grid
					matrix[k + row][k + col] = value + (k * k);
					// Add element in top right grid
					matrix[row][k + col] = value + (k * k * 2);
					// Add element in bottom left grid
					matrix[k + row][col] = value + (k * k * 3);
					col += 1;
					row -= 1;
					if (value % k == 0)
					{
						col -= 1;
						row += 2;
					}
					else
					{
						if (col == k)
						{
							col = 0;
						}
						else if (row < 0)
						{
							row += k;
						}
					}
					value += 1;
				}
			}
			else
			{
				// Doubly Even order magic square of n
				// (4*n)
				row = 0;
				while (row < n)
				{
					col = 0;
					while (col < n)
					{
						matrix[row][col] = (n * row) + col + 1;
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < n / 4)
				{
					col = 0;
					while (col < n / 4)
					{
						// Top Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 0;
				while (row < n / 4)
				{
					col = 3 * (n / 4);
					while (col < n)
					{
						// Top right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 3 * n / 4;
				while (row < n)
				{
					col = 0;
					while (col < n / 4)
					{
						// Bottom Left corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = 3 * n / 4;
				while (row < n)
				{
					col = 3 * n / 4;
					while (col < n)
					{
						// Bottom right corner
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
				row = n / 4;
				while (row < 3 * n / 4)
				{
					col = n / 4;
					while (col < 3 * n / 4)
					{
						// Centre elements
						matrix[row][col] = (n * n + 1) - matrix[row][col];
						col += 1;
					}
					row += 1;
				}
			}
			print(" Magic square of size (" + n + "X" + n + ") \n");
			// Display result
			row = 0;
			while (row < n)
			{
				col = 0;
				while (col < n)
				{
					print("  " + matrix[row][col]);
					col += 1;
				}
				print("\n");
				row += 1;
			}
			print(" Sum of each rows and columns is " + (n * (n * n + 1) / 2) + " \n\n");
		}
	}
}
fun main(args: Array < String > ): Unit
{
	var task: Square = Square();
	task.magicSquare(6);
	task.magicSquare(10);
	task.magicSquare(4);
	task.magicSquare(8);
}

Output

 Magic square of size (6X6)
  8  1  6  26  19  24
  3  5  7  21  23  25
  4  9  2  22  27  20
  35  28  33  17  10  15
  30  32  34  12  14  16
  31  36  29  13  18  11
 Sum of each rows and columns is 111

 Magic square of size (10X10)
  17  24  1  8  15  67  74  51  58  65
  23  5  7  14  16  73  55  57  64  66
  4  6  13  20  22  54  56  63  70  72
  10  12  19  21  3  60  62  69  71  53
  11  18  25  2  9  61  68  75  52  59
  92  99  76  83  90  42  49  26  33  40
  98  80  82  89  91  48  30  32  39  41
  79  81  88  95  97  29  31  38  45  47
  85  87  94  96  78  35  37  44  46  28
  86  93  100  77  84  36  43  50  27  34
 Sum of each rows and columns is 505

 Magic square of size (4X4)
  16  2  3  13
  5  11  10  8
  9  7  6  12
  4  14  15  1
 Sum of each rows and columns is 34

 Magic square of size (8X8)
  64  63  3  4  5  6  58  57
  56  55  11  12  13  14  50  49
  17  18  46  45  44  43  23  24
  25  26  38  37  36  35  31  32
  33  34  30  29  28  27  39  40
  41  42  22  21  20  19  47  48
  16  15  51  52  53  54  10  9
  8  7  59  60  61  62  2  1
 Sum of each rows and columns is 260


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved