C Program to Check whether entered matrix is magic square or not
Posted by Samath
Last Updated: January 03, 2017

Write a c program to check whether the given matrix is a magic square matrix or not? A magic square is a simple mathematical game developed during the 1500.Square is divided into equal number of rows and columns.Start filling each square with the number from 1 to num ( where num = No of Rows X No of Columns ).You can only use a number once.Fill each square so that the sum of each row is the same as the sum of each column.

Code:

```#include<stdio.h>
#include<conio.h>

int main() {
int size = 3;
int matrix[3][3];
int row, column = 0;
int sum, sum1, sum2;
int flag = 0;

for (row = 0; row < size; row++) {
for (column = 0; column < size; column++)
scanf("%d", &matrix[row][column]);
}

printf("Matrix: \n");
for (row = 0; row < size; row++) {
printf("\n");
for (column = 0; column < size; column++) {
printf("\t%d", matrix[row][column]);
}
}

sum = 0;
for (row = 0; row < size; row++) {
for (column = 0; column < size; column++) {
if (row == column)
sum = sum + matrix[row][column];
}
}

for (row = 0; row < size; row++) {
sum1 = 0;
for (column = 0; column < size; column++) {
sum1 = sum1 + matrix[row][column];
}
if (sum == sum1)
flag = 1;
else {
flag = 0;
break;
}
}

for (row = 0; row < size; row++) {
sum2 = 0;
for (column = 0; column < size; column++) {
sum2 = sum2 + matrix[column][row];
}
if (sum == sum2)
flag = 1;
else {
flag = 0;
break;
}
}

if (flag == 1)
printf("\nMagic square");
else
printf("\nNo Magic square");

return 0;
}```

C++ Magic Square
Samath | Jan 05, 2017
Write a C program to Print Square of Each Element of 2D Array Matrix
Samath | Jan 04, 2017
C Program to Calculate Area of Square
Samath | Jan 02, 2017
C program that multiplies the matrix a by the matrix b and stores the result in the matrix c
Samath | May 04, 2016
Square and Cube Class in C++
Samath | Jan 01, 2015
C Program to print table of n and square of n using pow() Function
Samath | Jan 02, 2017
Java program that calculates the square, cube, and fourth power of a number
Samath | Feb 08, 2021