C Program to Solve the Magic Squares Puzzle without Recursion

«
»

This is a C Program to solve the magic squares puzzle without using recursion.

Problem Description

The following C program, using iteration, finds the magic square for a given odd sized number.

Problem Solution

A magic square is an arrangement of numbers from 1 to n^2 in an [n x n] matrix, with each number occurring exactly once, and such that the sum of the entries of any row, any column, or any main diagonal is the same.

advertisement
Program/Source Code

Here is the source code of the C program to display a linked list in reverse. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

/*
 * C Program to Solve the Magic Squares Puzzle without using 
 * Recursion
 */
#include <stdio.h>
 
void magicsq(int, int [][10]);
 
int main( )
{
    int size;
    int a[10][10];
 
    printf("Enter the size: ");
    scanf("%d", &size);
    if (size % 2 == 0)
    {
        printf("Magic square works for an odd numbered size\n");
    }
    else
    {
        magicsq(size, a);
    }
    return 0;
}
 
void magicsq(int size, int a[][10])
{
    int sqr = size * size;
    int i = 0, j = size / 2, k;
 
    for (k = 1; k <= sqr; ++k) 
    {
        a[i][j] = k;
        i--;
        j++;
 
        if (k % size == 0) 
        { 
            i += 2; 
            --j; 
        }
        else 
        {
            if (j == size) 
            {
                j -= size;
            }
            else if (i < 0)
            {
                i += size;
            }
        }
    }
    for (i = 0; i < size; i++)
    {
        for (j = 0; j < size; j++)
        {
            printf("%d  ", a[i][j]);
        }
        printf("\n");
    }
    printf("\n");
}
Program Explanation

In this C Program, we are reading the size of an array using ‘size’ variable. If condition statement is used to check whether the size is odd numbered size or even numbered size. If the size is even numbered then magic square will not work for an even numbered and exit the program.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
advertisement
advertisement

Otherwise, if the condition is false, then enter the size is odd numbered size, hence magic square works for an odd numbered size. Execute the else statement. The magicsq() function is used to find the magic square for a given odd sized number.

Using for loop arrange the numbers from 1 to n^2 in an [n x n] matrix. If else condition statement is used to check that each number is occurring exactly once. Hence the sum of the entries of any row, any column, or any main diagonal is the same. Using for loop print the magic squares puzzle.

Runtime Test Cases
$ cc pgm27.c
$ a.out
Enter the size: 6
Magic square works for an odd numbered size
 
$ a.out
Enter the size: 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

Sanfoundry Global Education & Learning Series – 1000 C Programs.

advertisement

Here’s the list of Best Books in C Programming, Data-Structures and Algorithms

If you wish to look at other example programs on Puzzles & Games, go to Puzzles & Games. If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & technical discussions at Telegram SanfoundryClasses.