Advertisement
Mr_kindle

magicsquare.c

Nov 29th, 2022 (edited)
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.28 KB | None | 0 0
  1. /*
  2.     Name: magicsquare.c
  3.     Copyright:
  4.     Author: Mr.Kindle
  5.     Date: 29-11-22 23:24
  6.     Description: This program print the n*n magic square. Provided that 'n' must be an odd number.It also print the sum
  7.     of each row or column or digonals which are equal offcourse.
  8.  
  9. youtube: https://youtu.be/uVnsJjwLT4Y
  10. */
  11.  
  12.  
  13.  
  14. #include<stdio.h>
  15. #include<stdlib.h>
  16. #define MAX 15
  17.  
  18. void magicsquare( int);
  19.  
  20. void display();
  21.  
  22. /*taking mat and n as global variable so that all other function may use it*/
  23.  
  24. int MAT[MAX][MAX];
  25. int n;
  26.  
  27. int main()
  28. {  
  29.     int i,j,num;
  30.   int count=0;
  31.     printf("Enter 'n' (an odd number only) for n*n matrix: ");
  32.     scanf("%d",&n);
  33.    
  34.     if(n%2 == 0)//check for even number which is not acceptable
  35.         {
  36.             printf("\nPlease enter only odd number: ");
  37.             exit(1);
  38.         }
  39.    
  40.    
  41.     printf("Enter initial (or minimum range) number: ");
  42.     scanf("%d",&num);
  43.    
  44.     magicsquare( num);
  45.     printf("\n\n");
  46.     display();
  47.    
  48.     return 0;
  49.    
  50. }//main
  51.  
  52. void magicsquare( int num)
  53.     {
  54.         int i,j,no_of_term;
  55.         int term;
  56.         no_of_term = n*n;//total number of element
  57.          
  58.          /*setting INitial position of cursor*/
  59.          
  60.         i=(n-1)/2; //middle row
  61.         j=0; //first column
  62.    
  63.         for(term=1;term<=no_of_term;term++)
  64.         {
  65.             MAT[i][j]=num++;//first assign then increase
  66.      
  67.             if(term%n == 0)//if term is multiple of 'n'
  68.                j++; //take the cursor one step right
  69.             else
  70.             {   i++; //go down
  71.                 j--; //go left
  72.             }
  73.      /*if cursor comes out of the matrix*/
  74.      
  75.         if(i>n-1)
  76.             i=0;
  77.    
  78.         if(j<0)
  79.             j=n-1;
  80.    
  81.          }//for
  82.     }//end of magic square
  83.    
  84. void display()
  85.     {
  86.         int i,j,sum = 0;
  87.         for (i=0;i<n;i++)
  88.         {  
  89.             printf("|"); //for decoration  
  90.             for (j=0;j<n;j++)
  91.                  {
  92.                         printf(" %2d |",MAT[i][j]);
  93.                         if(i==0)
  94.                             {  
  95.                                 sum = sum + MAT[i][j];    
  96.                             }
  97.                 }
  98.             printf("\n");
  99.    
  100.         }
  101.     printf("\n\n sum of each row, each column or each digonal is = %d",sum);
  102.     }//end of display
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement