fork(21) download
  1. # include <stdio.h>
  2. # include <math.h>
  3.  
  4. int main() {
  5.  
  6. // Introductory message
  7. printf("This program creates a magic sqaure of a specified size.\n");
  8. printf("The size must be an odd number between 1 and 99.\n");
  9.  
  10. // Get the users magic number and allocate to int n
  11. int n;
  12. printf("Enter size of magic square: ");
  13. scanf("%d", &n);
  14. printf("\n");
  15. // Create the array (not using VLA)
  16. int magic[99][99], i, j;
  17. for( i = 0; i < n; i++ ) {
  18. for( j = 0; j < n; j++ ) {
  19. magic[i][j] = n * ( (i + j - 1 + (int) floor(n / 2)) % n ) + ((int) (i + 2 * j - 2) % n ) + 1;
  20. }
  21. }
  22.  
  23. // Now let's print the array
  24. for (i = 0; i < n; i++) {
  25. for (j = 0; j < n; j++) {
  26. printf("%4d", magic[i][j]);
  27. }
  28. printf("\n");
  29. }
  30. return 0;
  31. }
Success #stdin #stdout 0.01s 1680KB
stdin
5
stdout
This program creates a magic sqaure of a specified size.
The size must be an odd number between 1 and 99.
Enter size of magic square: 
   4  11  18  25   2
  10  17  24   1   8
  16  23   5   7  14
  22   4   6  13  20
   3  10  12  19  21