In this program, an array num of type int is used to store the given integer numbers and variable nelem to store their count. First, the value of nelem is read. Then a for loop is used to read the given numbers and store them in array num. Finally, another for loop is used to print the given numbers in reverse order.
Observe that in the second for loop, the array elements are accessed as num [i] and index variable i assumes the values nelem – l,nelem – 2, …,o. Thus, the array elements are printed in reverse order. Alternatively, we can print the array elements in reverse order using the for loop given below in which i assumes the values o, 1, …,nelem-1 and the array elements are referred as num [nelem – 1 – i]. The program is given below.
/* Print a list of numbers in reverse order */ #include<stdio.h> void main() { int num[50]; /* array with 50 elements */ int nelem, i; /* nelem: number of elements in array num */ /* read array */ clrscr(); printf("Enter number of array elements: "); scanf("%d", &nelem); printf("Enter array elements:\n"); for (i = 0; i < nelem; i++) scanf("%d", &num[i]); /* print array elements in reverse order */ printf("Array elements in reverse order:\n"); for (i = nelem - 1; i >= 0; i--) printf("%d ", num[i]); printf("\n"); getch(); }