This blog is under construction

Saturday, 27 July 2013

C program to print the given array in spiral form

Write a C program to print the given array in spiral form.


  #include <stdio.h>
  #define MAX 256

  int main() {
        int i, j, n, arr[MAX];

        /* get the number of array entries from the user */
        printf("Enter the number of array entries:");
        scanf("%d", &n);

        /* get the array entries from the user */
        printf("Enter your array entries:\n");
        for (i = 0; i < n; i++) {
                printf("Data[%d]: ", i);
                scanf("%d", &arr[i]);
        }

        /* print the array entries in spiral form */
        printf("Printing given array in spiral form:\n");
        for (i = 0; i < n; i++) {
                if (i % 2 == 0) {
                        printf("  %d\n", arr[i]);
                } else {
                        printf("    %d\n", arr[i]);
                }
        }

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of array entries:7
  Enter your array entries:
  Data[0]: 10
  Data[1]: 20
  Data[2]: 30
  Data[3]: 40
  Data[4]: 50
  Data[5]: 60
  Data[6]: 70 

  Printing given array in spiral form:
  10
      20
  30
      40
  50
      60
  70


No comments:

Post a Comment