52 lines
839 B
C
52 lines
839 B
C
#include <stdio.h>
|
|
|
|
#define N 3
|
|
|
|
int main () {
|
|
|
|
// int A[N][N] = {1,2,3,4,5,6,7,8,9}; // Initialisation pas magique
|
|
|
|
int A[N][N] = {4,2,3,2,3,4,3,4,2}; // Initialisation pas magique
|
|
|
|
int i , j;
|
|
|
|
// diagonale
|
|
|
|
int sommed1 = 0, sommed2 = 0;
|
|
|
|
for(i=0;i<N;i=i+1)
|
|
{
|
|
sommed1 = sommed1 + A[i][i];
|
|
sommed2 = sommed2 + A[i][N-1-i];
|
|
}
|
|
|
|
if ( sommed1 != sommed2 )
|
|
{
|
|
printf("stop, pas magique\n");
|
|
return 1;
|
|
}
|
|
|
|
// hors diagonale
|
|
int sommeV , sommeH ;
|
|
for(i=0;i<N;i=i+1)
|
|
{
|
|
sommeH = 0 ;
|
|
sommeV = 0 ;
|
|
for (j=0;j<N;j++)
|
|
{
|
|
sommeH = sommeH + A[i][j] ;
|
|
sommeV = sommeV + A[j][i] ;
|
|
}
|
|
if ( sommeH != sommed1 || sommeV != sommed1 )
|
|
{
|
|
printf("stop, pas magique\n");
|
|
return 1;
|
|
}
|
|
|
|
}
|
|
|
|
printf("carre magique = %d \n",sommed1);
|
|
return 1;
|
|
|
|
}
|