C++ Program to design a Magic Square [DEVCPP/GCC]

PROGRAM

//Program to design a Magic Square.

#include<iostream>
using namespace std;

int main()
{
int k[4][4],i,j,a,b,c,d,p,h;
int sum1,sum2,sum3,sum4;
char ch='n';

     cout<<"ENTER THE VALUES OF a,b,c,d"<<endl;
cout<<"a:";
cin>>a;
cout<<"\nb:";
cin>>b;
cout<<"\nc:";
cin>>c;
cout<<"\nd:";
cin>>d;

     cout<<"\nSUM OF ELEMENTS ENTERED(a,b,c,d):"<<(a+b+c+d)<<endl;
cout<<"DESIGNED A MAGICAL SQUARE USING THE ENTERED NUMBERS(a,b,c,d)..." <<endl<<endl;

    k[0][0]=a;    k[0][1]=b;    k[0][2]=c;    k[0][3]=d;
k[1][0]=d+1;  k[1][1]=c-1;  k[1][2]=b-3;  k[1][3]=a+3;
k[2][0]=b-2;  k[2][1]=a+2;  k[2][2]=d+2;  k[2][3]=c-2;
k[3][0]=c+1;  k[3][1]=d-1;  k[3][2]=a+1;  k[3][3]=b-1;    //Formula for magic square
for(i=0;i<4;i++)
{
for(j=0;j<4;j++)
{
cout<<k[i][j]<<"\t";
}
cout<<endl;
    }
   
 
     cout<<"\nTHIS IS THE MAGICAL SQUARE\n"<<endl;
     cout<<"IN THIS MAGICAL SQUARE THE SUM OF ELEMENTS OF EACH ROW\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF EACH COLUMN\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF EACH DIAGONAL\n"<<endl;
     cout<<"THE SUM OF ELEMENTS OF MIDDLE SQUARE\n"<<endl;
     cout<<"WILL BE SAME. AND IT IS EQUAL TO THE SUM OF THE ELEMENTS ENTERED."<<endl<<endl;
     cout<<"LET US SEE..."<<endl;
     cout<<"FROM WHERE U WANNA START:\n\n";
 
     do
     {
   
             cout<<"1.ROWS\n";
             cout<<"2.COLUMNS\n";
             cout<<"3.DIAGONALS\n";
             cout<<"4.MIDDLE SQUARE\n";
             cout<<"ENTER YOUR CHOICE:\n";
             cin>>h;
 
             switch(h)
             {
   
                   case 1:
   
do
{
                          cout<<"ROWS\n";
                          cout<<"WE HAVE 4 ROWS 1,2,3,4. ENTER THE NO OF ROW WHOSE SUM OF ELEMENTS U WANT TO SEE\n";
                          cin>>p;
   
                           switch(p)
    {
   
                                 case 1:

                                 i=0; sum1=0;
                               for(j=0;j<4;j++)
  {
  sum1=sum1+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST ROW:"<<sum1<<endl;
break;
case 2:

                                 i=1; sum2=0;
                               for(j=0;j<4;j++)
  {
  sum2=sum2+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 2ND ROW:"<<sum2<<endl;
break;
case 3:

                                  i=2; sum3=0;
                                for(j=0;j<4;j++)
  {
  sum3=sum3+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 3RD ROW:"<<sum3<<endl;
break;
                                 case 4:
   
                                   i=3; sum4=0;
                                 for(j=0;j<4;j++)
  {
  sum4=sum4+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 4Th ROW:"<<sum4<<endl;
break;
default :
cout<<"default";
break;
   }

cout<<"DO U WISH TO CONTINUE WITH OTHER ROWS:"<<endl;
cin>>ch;
   
    }while(ch=='Y'||ch=='y');
break;
case 2:
do
{
cout<<"COLUMNS"<<endl;
cout<<"WE HAVE 4 COLUMNS 1,2,3,4. ENTER THE NO OF COLUMN WHOSE SUM OF ELEMENTS YOU WANT TO SEE\n";
cin>>p;

              switch(p)
{
case 1:

                    j=0; sum1=0;
for(i=0;i<4;i++)
{
sum1=k[i][j]+sum1;
}
cout<<"SUM OF ELEMENT OF 1ST COLUMN:"<<sum1<<endl;
break;
case 2:

                     j=1; sum2=0;
for(i=0;i<4;i++)
{
sum2=k[i][j]+sum2;
}
cout<<"SUM OF ELEMENT OF 2ND COLUMN:"<<sum2<<endl;
break;
case 3:

                     j=2; sum3=0;
for(i=0;i<4;i++)
{
sum3=k[i][j]+sum3;
}
cout<<"SUM OF ELEMENT OF 3RD COLUMN:"<<sum3<<endl;
break;
case 4:

                      j=3; sum4=0;
for(i=0;i<4;i++)
{
sum4=k[i][j]+sum4;
}
cout<<"SUM OF ELEMENT OF 4TH COLUMN:"<<sum4<<endl;
break;
default:
cout<<"default";
break;
}
cout<<"DO YOU WISH TO CONTINUE WITH COLUMNS... "<<endl;
cin>>ch;

  }while(ch=='Y'||ch=='y');
break;
case 3:
do
{

                cout<<"DIAGONALS"<<endl;
cout<<"WE HAVE TWO DIAGONALS 1,2.ENTER THE DIADONAL WHOSE SUM OF ELEMENTS YOU WANT TO SEE\n";
cin>>p;

                switch(p)
{

                       case 1:

                       sum1=0;
for(i=0,j=0;i<4;i++,j++)
{
sum1=sum1+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST DIAGONAL:"<<sum1<<endl;
break;
case 2:

                        sum2=0;
for(i=0,j=3;i<4;i++,j--)
{
sum2=sum2+k[i][j];
}
cout<<"SUM OF ELEMENTS OF 1ST DIAGONAL:"<<sum2<<endl;
break;
default :
cout<<"DEFAULT";
break;
}
cout<<"DO WISH TO CONTINUE WITH ANOTHER DIAGONAL..."<<endl;
cin>>ch;

  }while(ch=='Y'||ch=='y');
break;
case 4:

          sum1=0;
cout<<"MIDDLE INNER SQUARE";

          for(i=1;i<3;i++)
{
for(j=1;j<3;j++)
{
sum1=sum1+k[i][j];
}
}
cout<<"SUM OF ELEMENTS OF MIDDLE SQUARE IS:"<<sum1<<endl;
break;
      }
      cout<<"DO YOU HAVE SOME OTHER CHOICE:"<<endl;
      cin>>ch;
}
while(ch=='Y'||ch=='y');

}

OUTPUT




   

FIND US ON FACEBOOK!