Turn Desktop View Off
by Dinesh Thakur Category: Array

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();

}

        Print a list of numbers in reverse order



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.



Related Articles