Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*This code print only 4*4 magic square. It is a hardcode program.*/
- #include<stdio.h>/*this program is done by myself without any assistance*/
- # define SIZE 20
- int main()
- { int i,j,i_no,temp, sum=0;
- int a[20][20];
- printf("Enter the initial value: ");
- scanf("%d",&i_no);
- //first of all make a 4*4 matrix
- for(i=0;i<4;i++)
- for(j=0;j<4;j++)
- {
- a[i][j]=i_no;
- i_no++;
- }
- //Now to make magic square digonals values should be interchanged in the following order.
- //a[0][0] with a[3][3], a[1][1] with a[2][3], do the same thing in other digonal as well
- temp = a[0][0];
- a[0][0] = a[3][3];
- a[3][3]= temp;
- //a[1][1] with a[2][2]
- temp = a[1][1];
- a[1][1] = a[2][2];
- a[2][2]= temp;
- //a[0][3] with a[3][0]
- temp = a[0][3];
- a[0][3] = a[3][0];
- a[3][0]= temp;
- //a[1][2] with a[2][1]
- temp = a[1][2];
- a[1][2] = a[2][1];
- a[2][1]= temp;
- printf("\n Here is your 4*4 magic square: \n");
- for(i=0;i<4;i++)
- {
- for(j=0;j<4;j++)
- printf("%2d ",a[i][j]);
- printf("\n");
- }
- for(i=0;i==0;i++)
- for(j=0;j<4;j++)
- sum+=a[i][j];
- printf("\n\n");
- printf("\n\n Sum of any row or coloumn or digonal = %d\n",sum);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement