Python program to print the elements of an array in reverse order

python tutorials and learn python

Created with Sketch.

 Python program to print the elements of an array in reverse order

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.

 

 

 

ALGORITHM:

  • STEP 1: Declare and initialize an array.
  • STEP 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.
  • STEP 3: Print the element arr[i] in each iteration.

PROGRAM:

  1. #Initialize array   
  2. arr = [12345];
  3. print(“Original array: “);
  4. for i in range(0, len(arr)):
  5.     print(arr[i]),
  6. print(“Array in reverse order: “);
  7. #Loop through the array in reverse order  
  8. for i in range(len(arr)-1, –1, –1):
  9.     print(arr[i]),

Output:

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

Leave a Reply

Your email address will not be published. Required fields are marked *