Introduction :
There are different type of puzzle you solve in your life. A magic square is one of them. A magic square is a square grid of numbers where the sums of each row, column and diagonal is always the same. You can generate a magic square using C or C++ programming language.
In this article, I show you how build a magic square of order 4 by C or C++ programming language. Here, the user just input a starting number and creates a magic square of order 4 which display on the console.
About the program :
This is a simple magic square program build by C or C++ programming language. In the program, you have to input a number. This is the starting number of the magic square. After that, you see a magic square in the console. When you press any key, the program will be stopped.
Explanation of the program :
At the beginning of the program, you have to include header Files such as stdio.h and conio.h for standard and console input and output operations respectively. Now, declare swap function which takes two integer pointers as arguments and swaps the values of elements of the array. In the main function, declare and initialize array and integer variables.
Here, you have to declare the array (4×4) “n” to store the value of magic square. You have to also declare various loop control variables and other necessary integers like “i”, “j”, “num”, “k” and “l”. To clear the console screen, you can use clrscr() function. Using printf() function, asked the user to enter any number which is used as the starting number of the magic square. You can store the starting number in “num” variable by scanf() function.
Now, for loop use to fill the array “n” with numbers starting from the user input value. After that, using swap() function swaps on specific elements of the array to transform it into a magic square. Using clrscr() and printf() functions, clear the console screen and print a message on the console. The gotoxy() use for positions the cursor specific coordinate.
Using nested loops, you can display the magic square on the console. Here, you have to draw horizontal and vertical lines using loops and ASCII characters to separate rows and columns in the console output. You can use getch() function to get a key press from user before stopping the program.
How run the program :
To run the program on your, first install the Turbo C++ IDE. Now, open the Turbo C++ IDE and create a C or C++ file with .c or .cpp extension. Now, copy and paste the below code in your C or C++ file which you just created. Do you know how to copy paste in the Turbo C++ IDE, if you do not know click here. After that you can run the program on your pc.
Source code :
The following code is the source code of the generate magic square.
/*Developed by Puskar Jasu*/
#include <stdio.h>
#include <conio.h>
void swap(int *p, int *q)
{
int t;
t = *p;
*p = *q;
*q = t;
}
int main()
{
int n[4][4], i, j, num, k = 0, l = 0;
int ch;
clrscr();
printf("enter the stating number\n");
scanf("%d", &num);
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
n[i][j] = num;
num++;
}
}
swap(&n[0][3], &n[3][0]);
swap(&n[1][2], &n[2][1]);
swap(&n[0][0], &n[3][3]);
swap(&n[1][1], &n[2][2]);
clrscr();
printf("the magic square is\n\n");
gotoxy(20, 5);
k = 0;
for (i = 0; i < 4; i++)
{
for (j = 0; j < 4; j++)
{
gotoxy(21 + k, 7 + l);
printf("%d ", n[i][j]);
k += 5;
}
k = 0;
l += 2;
}
for (j = 0; j < 5; j++)
{
for (i = 0; i < 20; i++)
{
gotoxy(20 + i, 6 + k);
printf("%c", 196);
}
k += 2;
}
k = 0;
for (j = 0; j < 5; j++)
{
for (i = 0; i < 9; i++)
{
gotoxy(20 + k, 6 + i);
printf("%c", 179);
}
k += 5;
}
getch();
return 0;
}
Output :
After run the magic square program, you can see the result on the console like this.

Conclusion :
At last, you have learned how generates a magic square by C or C++ programming language. Now, you can create different types of magic square using mathematical and programming skills. Thanks you for visiting my site.