Wednesday, 24 January 2018

How to make Magic Square in C-Language using 2-D array





#include <stdio.h>

#include <conio.h>



main()

{



int x[3][3]{{2,7,6},{9,5,1},{4,3,8}};

int row[3];

int col[3];

int d1=0,d2=0,i,j,flag=1;



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

{

row[i]=0;

col[i]=0;



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

{

row[i]=row[i]+x[i][j];

col[i]=col[i]+x[j][i];



if(i==j)

{

d1=d1+x[i][j];

}



if(i+j==2)

{

d2=d2+x[i][j];

}



} // inner loop end......



} //outer loop end











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

{



if(row[i]!=col[i])

{

flag=0;

break;

}



}



if(d1!=d2)

{

flag=0;

}



if(flag==0)

{



printf("It is not a magic square.....");

}

else

{

printf("It is a magic square.....");



}

















}

No comments:

Post a Comment

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...