Program to print array in C



This program will let you understand that how to print an array in C. We need to declare & define one array and then loop upto the length of array. At each iteration we shall print one index value of array. We can take this index value from the iteration itself.

Algorithm

Let's first see what should be the step-by-step procedure of this program −

START
   Step 1 → Take an array A and define its values
   Step 2 → Loop for each value of A
   Step 3 → Display A[n] where n is the value of current iteration
STOP

Pseudocode

Let's now see the pseudocode of this algorithm −

procedure print_array(A)

   FOR EACH value in A DO
      DISPLAY A[n]
   END FOR

end procedure

Implementation

The implementation of the above derived pseudocode is as follows −

#include <stdio.h>

int main() {
   int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 0};
   int loop;

   for(loop = 0; loop < 10; loop++)
      printf("%d ", array[loop]);
      
   return 0;
}

The output should look like this −

1 2 3 4 5 6 7 8 9 0
array_examples_in_c.htm
Advertisements