Advertisement
Mr_kindle

4*4 magic square.c

Nov 19th, 2024
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.17 KB | Source Code | 0 0
  1.  
  2.  
  3. /*This code print only 4*4 magic square. It is a hardcode program.*/
  4. #include<stdio.h>/*this program is done by myself without any assistance*/
  5. # define SIZE 20
  6. int main()
  7. { int i,j,i_no,temp, sum=0;
  8.     int a[20][20];
  9.     printf("Enter the initial value:  ");
  10.     scanf("%d",&i_no);
  11.     //first of all make a 4*4 matrix
  12.     for(i=0;i<4;i++)
  13.         for(j=0;j<4;j++)
  14.             {
  15.                 a[i][j]=i_no;
  16.                 i_no++;
  17.             }
  18.     //Now to make magic square digonals values should be interchanged in the following order.
  19.     //a[0][0] with a[3][3], a[1][1] with a[2][3], do the same thing in other digonal as well
  20.     temp = a[0][0];
  21.     a[0][0] = a[3][3];
  22.     a[3][3]= temp;
  23.     //a[1][1] with a[2][2]
  24.     temp = a[1][1];
  25.     a[1][1] = a[2][2];
  26.     a[2][2]= temp;
  27.     //a[0][3] with a[3][0]
  28.     temp = a[0][3];
  29.     a[0][3] = a[3][0];
  30.     a[3][0]= temp;
  31.     //a[1][2] with a[2][1]
  32.     temp = a[1][2];
  33.     a[1][2] = a[2][1];
  34.     a[2][1]= temp;
  35.    
  36.    
  37.         printf("\n Here is your 4*4 magic square: \n");
  38.     for(i=0;i<4;i++)
  39.     {
  40.             for(j=0;j<4;j++)
  41.                 printf("%2d ",a[i][j]);
  42.             printf("\n");
  43.     }
  44.     for(i=0;i==0;i++)
  45.             for(j=0;j<4;j++)
  46.                 sum+=a[i][j];
  47.                 printf("\n\n");
  48.     printf("\n\n Sum of any row or coloumn or digonal = %d\n",sum);
  49.     return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement