SCHOOL OF CODE BUILDERS
Learn To CODE. Become A DEVELOPER.
Pages
HOME
DATA STRUCTURES
STRINGS
ARRAYS
MATRIX
BINARY TREES
LINKED LIST
STACK
QUEUE
SORTING
SEARCHING
C
PYTHON
PSEUDOCODE
CONTEST PROBLEMS
ALGORITHMS
PATTERNS
PHP
C PUZZLES
C INTERVIEW QUESTIONS
JAVA
C++
HASHING
RECURSION
BASIC C PROGRAMS
TCS-CODEVITA
FACEBOOK
CONTACT US
C program to find magic square matrix
C program to find magic square matrix
Output:
The given matrix is magic square matrix
Implementation:
#include
#define r 3 #define c 3 int main(void) { int a[r][c]={{4,9,2}, {3,5,7}, {8,1,6}}; int i,j,k; int flag=1; int sum[r+c+2]={0}; /* Magic square matrix is the matrix with row sum = column sum = diagonal sum for example: take the matrix: 4 9 2 3 5 7 8 1 6 It has row sums as: row1 = 4 + 9 + 2 = 15 row2 = 3 + 5 + 7 = 15 row3 = 8 + 1 + 6 = 15 It has column sums as: column1 = 4 + 3 + 8 = 15 column2 = 9 + 5 + 1 = 15 column3 = 2 + 7 + 6 = 15 Finally, left diagonal sum = 4 + 5 + 6 = 15 right diagonal sum = 2 + 5 + 8 = 15 Since, all the rows & columns & diagonals have same sum Hence, the matrix is a magic square matrix with all values from 1 to 9(rows*columns = 3 * 3) and placed in the matrix exactly only once */ k=0; /* checking for rows */ for(i=0;i
PREVIOUS
NEXT
HOME