This blog is under construction

Tuesday 23 July 2013

C program to display array elements

Write a C program to print array elements.


  #include <stdio.h>
  #include <stdio.h>
  #define MAXLIMIT 256
  int main() {
        int data[MAXLIMIT], i, n;

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

        /* boundary check */
        if (n > MAXLIMIT || n < 0) {
                printf("Boundary Exceeded!!\n");
                return 0;
        }

        /* get the array inputs from the user */
        printf("\nEnter the input data:\n");
        for (i = 0; i < n; i++) {
                printf("Data[%d]: ", i);
                scanf("%d", &data[i]);
        }

        /* print the elements in the array */
        printf("\nElements in the given array:\n");
        printf("{");
        for (i = 0; i < n; i++) {
                printf("%d, ", data[i]);
        }
        printf("\b\b }\n");

        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/cpgms/lab_pgms/06_arrays$ ./a.out
  Enter the number of elements:6
  Enter the input data:
  Data[0]: 1
  Data[1]: 2
  Data[2]: 3
  Data[3]: 4
  Data[4]: 5
  Data[5]: 6
  
  Elements in the given array:
  {1, 2, 3, 4, 5, 6 }


No comments:

Post a Comment