C++ Programming GalleryThe Programming Challenge
Challenge 16 – Solve the “magic square” of order N in C++

Difficulty ![]() |
![]() |
This Challenge was proposed for the now defunct C/C++ programming list CWORLD in 1999.
The source code of the challenge has been updated to modern C++, but maintaining the essence of the challenge, which its original author wanted to transmit.
Date: 17-IV-2001
Created by: Fabian Silva Pavez
Challenge winner: Antonio González Cruz
Now I propose to give a solution on how to solve the “magic square”
p.e. N=3
8 1 6
3 5 7
4 9 2
sum = 15
so that the vertical, horizontal and diagonal lines of length 3 (in this case) add up to the same, in the same way “take out” prints the sum that it should have…
The program must solve squares of any size, odd number and greater than 2…
p.e. N=5
17 24 1 8 15
23 5 7 14 16
4 6 13 20 22
10 12 19 21 3
11 18 25 2 9
sum = 65
AND FINALLY I CAN ONLY SAY …. BEST OF LUCK AND MAY THE BEST CODE WIN….
If you liked this challenge remember to leave a comment in our guestbook