Home » Program to print the elements of an array in reverse order

Program to print the elements of an array in reverse order

by Online Tutorials Library

Q. Program to print the elements of an array in reverse order.

Explanation

In this program, we need to print the elements of the array in reverse order that is; the last element should be displayed first, followed by second last element and so on.

Program to print the elements of an array in reverse order

Above array in reversed order:

Program to print the elements of an array in reverse order

Algorithm

  1. Declare and initialize an array.
  2. Loop through the array in reverse order that is, the loop will start from (length of the array – 1) and end at 0 by decreasing the value of i by 1.
  3. Print the element arr[i] in each iteration.

Solution

Python

Output:

Original array:   1 2 3 4 5   Array in reverse order:   5 4 3 2 1   

C

Output:

Original array:   1 2 3 4 5   Array in reverse order:   5 4 3 2 1   

JAVA

Output:

Original array:   1 2 3 4 5   Array in reverse order:   5 4 3 2 1   

C#

Output:

Original array:   1 2 3 4 5   Array in reverse order:   5 4 3 2 1   

PHP

Output:

Original array:   1 2 3 4 5   Array in reverse order:   5 4 3 2 1   

Next Topic#

You may also like