As early as 650 BC mathematicians had been composing magic s
As early as 650 BC, mathematicians had been composing magic squares, a sequence of n number arranged in a square such that all rows, columns, and diagonals sum to the same constant. Used in China, India, and Arab countries for centuries, artist Albrecht Durer’s engraving Melecolia 1(year: 1514) is considered the first time a magic square appears in European art. Each row, column, and diagonal of Durer’s magic square sums to 34. In addition, each quadrant, the center four squares, and the corner squares all sum to 34. An example of a “magic square” is displayed below.
16 3 2 13
5 10 11 8
9 6 7 12
4 15 14 1
Write a program to prove a series of numbers is indeed a 4x4 magic square. Your program should complete the following steps, in order:
Ask the user to enter their proposed magic square in a single input statement (e.g.,[1 2 3 4;5 6 7 8;9 10 11 12; 13 14 15 16]-note this example is a 4x4 matrix, but not a magic square). You may assume the user will enter whole numbers: they will not enter either decimal values or text.
Check that all values are positive: for loop or nested for loop required in the solution. If one or more of the values in the matrix are negative, or zero, issue a statement to the command window informing the user of the mistakes and ask the user to enter another matrix. This check should be repeated until the user enters a matrix with positive values. This check should work even if the user does not enter 4x4 matrix: it should work regardless of the size of matrix entered.
Check for an arrangement of 4x4. If the matrix is not a 4x4, issue a statement to the command window informing the user of the mistake and ask the user to enter another matrix. This check should be repeated until the user enters a 4x4 matrix. You many assume the re-entered matrix contains only positive values; you do not need to re-check the new matrix for positive values, only for matrix dimensions.
Determine if the matrix is a form of a magic square. The minimum requirement to be classified as a magic square is each row and column sums to the same value. For loop or nested for loop required in the solution, If this criteria is not met, issue a statement to the command window informing the user they have not entered a magic square and ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step(a) should begin again.
If each row and column sums to the same value, the magic square is classified as “semimagic”: the summation value is called the magic constant.
If, in addition to criterion 1, each diagonal sums to the same value as the row and columns, the magic square is classified as “normal”, For loop or nested for loop required in the solution the use of built-in functions such as diag, fliplr, rot90, trace or similar built in functions is forbidden.
If in addition to #1 and #2, the largest value in the magic square is equal to 16, the magic square is classified as “perfect”
Format your magic square classification similar to the format shown below. You may choose to format your table differently, but each classification shoul conatin a “yes” or “no” next to each magic square category.
The magic constant for your magic square is 24. The classification for your magic square
Semi-Magic
Normal
Perfect
yes
yes
yes
After this table appears, ask the user if they wish to try another magic square. This question can be posed using either a text answer entered by the user (yes, no) or by using a menu. If the user chooses to run the program again, the entire program starting with step (a) should begin again.
 A few test cases for you to consider:
Albrecht Durer magic square: [16, 3, 2, 13; 5, 10, 11, 8; 9, 6, 7, 12; 4, 15, 14, 1];
 Chautisa Yantra magic square: [7, 12, 1, 14; 2, 13, 8, 11; 16, 3, 10, 5; 9, 6, 15, 4];
 Sangrada Familia church, Barcelona magic square: [1, 14, 14, 4; 11, 7, 6, 9; 8, 10, 10, 5; 13, 2, 3, 15]; Random magic square: [80, 15, 10, 65; 25, 50, 55, 40; 45, 30, 35, 60; 20, 75, 70, 5];
 Steve Wozniak’s magic square: [8, 11, 22, 1; 21, 2, 7, 12; 3, 24, 9, 6; 10, 5, 4, 23]
| Semi-Magic | Normal | Perfect | 
| yes | yes | yes | 
Solution
A code was written in Matlab. The codes are as follows:
Magic_number.m-main program
% Program to check whether the function is a magic square
flag1=1; % Flag to check exit loop condition
 while flag1==1
 [flag2,A]=inp();
 if flag2==1
 flag3=check(A);
 if flag3(1)==1
 display(\'Semi magic Yes\');
 else
 display(\'Semi magic No\');
 end
 if flag3(1)==1
 display(\'Normal yes\');
 else
 display(\'Normal No\');
 end
 if flag3(1)==1
 display(\'Perfect Yes\');
 else
 display(\'Perfect No\');
 end
 elseif flag2==2
 display(\'non positive elements found. Please re-enter the matrix\');
 elseif flag2==3
 display(\'size of matrix is not 4x4\');
 end
 if flag2==1
 flag4=input(\'Press 1 if you want to try another matrix. Else press 0 : \');
 if flag4==1
 flag1=1;
 else
 flag1=0;
 end
 end
 end
inp.m - function to get inputs
% Function to obtain input and check whether the input matrix is acceptable
 function [flag,A]=inp()
 A=input(\'Enter the matrix in consideration: \');
 s=size(A);
 flag=1;
 for i=1:1:s(1)
 for j=1:1:s(1)
 if A(i,j)<=0
 flag=2;
 end
 end
 end
 if flag~=2
 if s(1)~=4
 flag=3;
 elseif s(2)~=4
 flag=3;
 end
 end
check.m - function to check whether the matrix is a magic square
%Function to check whether a matrix is magic square
 function flag=check(A)
 flag=[1,1,1];
 sum=0;
 sum_temp=0;
 for i=1:1:4
 sum_row=0;
 for j=1:1:4
 sum_row=sum_row+A(i,j);
 end
 if i==1
 sum=sum_row;
 elseif sum_row~=sum
 flag(1)=0;
 end
 end
 if flag(1)==1
 for i=1:1:4
 sum_col=0;
 for j=1:1:4
 sum_col=sum_col+A(j,i);
 end
 if sum_row~=sum
 flag(1)=0;
 end
 end
 else
 flag(2)=0;
 flag(3)=0;
 end
 if flag(1)==1
 sum_diag=0;
 for i=1:1:4
 sum_diag=sum_diag+A(i,i);
 end
 if sum_diag~=sum
 flag(2)=0;
 flag(3)=0;
 else
 max=0;
 for i=1:1:4
 for j=1:1:4
 if max<A(i,j)
 max=A(i,j);
 end
 end
 end
 if max~=16
 flag(3)=0;
 end
 end
 end



