0% found this document useful (0 votes)
55 views6 pages

Magic Square: - : Code

The document contains code to check if a matrix is a magic square. It takes input for the matrix elements, calculates the sums of rows, columns and diagonals, and compares the sums to determine if it is a magic square. It will only accept odd number of rows, and restarts if an even number is entered. If all sums are equal, it outputs that the matrix is a magic square, otherwise it is not a magic square.

Uploaded by

Ninad Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
55 views6 pages

Magic Square: - : Code

The document contains code to check if a matrix is a magic square. It takes input for the matrix elements, calculates the sums of rows, columns and diagonals, and compares the sums to determine if it is a magic square. It will only accept odd number of rows, and restarts if an even number is entered. If all sums are equal, it outputs that the matrix is a magic square, otherwise it is not a magic square.

Uploaded by

Ninad Joshi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

MAGIC SQUARE: -

CODE: -
#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

int main()

system("cls");

int i,j,rows,col,diagonal_1=0,diagonal_2=0,m,n,k=0,sum_rows=0,sum_col=0,flag=0;

printf("[+]ENTER THE NUMBER OF ROWS =>");

scanf("%d",&rows);

col=rows;

int a[rows][col];

if(rows%2!=0)

m=0;

n=rows-1;

//Taking input for Magic Square

printf("Enter Magic Square Numbers Horizontally\n");

//If 8 1 6

// 3 5 7

// 4 9 2

//Then Enter from 8 to 6 and then 3 to 7 and then 4 to 9

for(i=0;i<rows;i++)

for(j=0;j<col;j++)

printf("a[%d][%d] =>",i+1,j+1);

scanf("%d",&a[i][j]);

}
}

printf("\n[+]THE GIVEN MATRIX IS\n\n=======================\n\n");

for(i=0;i<rows;i++)

for(j=0;j<col;j++)

printf("%d\t",a[i][j]);

printf("\n");

printf("\n\n=======================\n\n");

//Finding Sum for 2 Diagonals

for(i=0;i<rows;i++)

diagonal_1+=a[i][i];

diagonal_2+=a[m][n];

m++;

n--;

printf("\n[+]Sum of diagonal 1 is %d\n\n",diagonal_1);

printf("[+]Sum of diagonal 2 is %d\n\n",diagonal_2);

if(diagonal_2==diagonal_1)

flag=1;

else

flag=0;

printf("\n[+]Diagonals Sum are not Equal so Not A MagicSquare\n\n");

exit(0);

}
//Finding Sum of Rows

for(i=0;i<rows;i++)

sum_rows=0;

for(j=0;j<col;j++)

sum_rows+=a[i][j];

printf("[+]Sum of %d Row is %d\n\n",k+1,sum_rows);

k++;

if(diagonal_1==sum_rows)

flag=1;

else

flag=0;

printf("\n\n[+]Sum of Rows and Diagonals are not Equal,so Not A MagicSquare\n\n");

exit(0);

//Finding sum of Columns

k=0;

for(i=0;i<col;i++)

sum_col=0;

for(j=0;j<rows;j++)

sum_col+=a[j][i];
}

printf("\n[+]Sum of %d Column is %d\n\n",k+1,sum_col);

k++;

if(diagonal_1==sum_col)

flag=1;

else

flag=0;

printf("\n\n[+]Even Though Diagonals and Rows Sum are Equal but Sum of Columns
are not Equal,so Not A MagicSquare\n\n");

exit(0);

if(flag==1)

printf("\n\n[+]Given Matrix is Magic Square\n\n");

else

printf("\n\n[+]Not Possible as rows has to be Odd\n\n");

printf("\n[+]RESTARTING THE PROGRAM!! PRESS ENTER TO RESTART THE


PROGRAM!!\n");

getch();

return main() ;

return(0);

ALGORITHM: -
1) First the user will input the number of rows.
2) Then it will check the whether the number of rows is odd or not.
3) If it is odd, it will accept the elements for matrix.
4) But if it’s not, then it prompts to enter the odd number and restart the program.
5) When the user has entered the elements, the sum of elements of each row, column
and both the diagonals.
6) Then it will compare all the sums.
7) If all the sums are equal, then it will display that it is a magic square.
8) But if they are not equal then it will display that it is not a magic square.

OUTPUTS: -

You might also like