/*
Name: Magic Square.
Author: Ketan Kulkarni
Date: 17-10-19 00:11
SPPU assignment no.3
Description: Write a c++ program to
implement the magic square.
*/
#include
<iostream>
using
namespace std;
int
main()
{
int i,j,l,k,size=3,row,col,m,nr,nc;
int matrix[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
matrix[i][j]=0;
}
}
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<matrix[k][l]<<"\t";
}
cout<<"\n";
}
if(size%2==0)
{
cout<<"\n matrix
is not possible";
}
nr=0;
nc=(int)size/2;
matrix[nr][nc]=1;
for(m=2;m<=3*3;m++)
{
row=nr;
col=nc;
nr=nr-1;
if(nr<0)
nr=size-1;
nc=nc-1;
if(nc<0)
nc=size-1;
if(matrix[nr][nc]!=0)
{
nr=++row;
nc=col;
}
matrix[nr][nc]=m;
}
cout<<"\n";
for(k=0;k<3;k++)
{
for(l=0;l<3;l++)
{
cout<<matrix[k][l]<<"\t";
}
cout<<"\n";
}
return 0;
}
/*
0 0
0
0 0
0
0 0
0
6 1
8
7 5
3
2 9
4
--------------------------------
*/
Comments
Post a Comment