TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Program to print the elements of an array in reverse order

Program to print the elements of an array in reverse order on fibonacci, factorial, prime, armstrong, swap, reverse, search, sort, stack, queue, array, linkedlist, tree, graph etc.

<< Back to PROGRAM

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

#Initialize array 
arr = [1, 2, 3, 4, 5]; 
print("Original array: ");
for i in range(0, len(arr)):
    print(arr[i]), 
print("Array in reverse order: ");
#Loop through the array in reverse order
for i in range(len(arr)-1, -1, -1): 
    print(arr[i]), 

Output:

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

C

#include <stdio.h>
 
int main()
{
    //Initialize array 
    int arr[] = {1, 2, 3, 4, 5}; 
    
    //Calculate length of array arr
    int length = sizeof(arr)/sizeof(arr[0]);
    
    printf("Original array: \n");
    for (int i = 0; i < length; i++) { 
        printf("%d ", arr[i]); 
    }  
    
    printf("\n");
    
    printf("Array in reverse order: \n");
    //Loop through the array in reverse order
    for (int i = length-1; i >= 0; i--) { 
        printf("%d ", arr[i]); 
    } 
    return 0;
}

Output:

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

JAVA

public class ReverseArray {
    public static void main(String[] args) {    
        
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        System.out.println("Original array: ");
        for (int i = 0; i < arr.length; i++) { 
            System.out.print(arr[i] + " "); 
        }  
        
        System.out.println();
        
        System.out.println("Array in reverse order: ");
        //Loop through the array in reverse order
        for (int i = arr.length-1; i >= 0; i--) { 
            System.out.print(arr[i] + " "); 
        }  
    }
}

Output:

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

C#

using System;
                    
public class ReverseArray
{
    public static void Main()
    {
        //Initialize array 
        int [] arr = new int [] {1, 2, 3, 4, 5}; 
        
        Console.WriteLine("Original array: ");
        for (int i = 0; i < arr.Length; i++) { 
            Console.Write(arr[i] + " "); 
        }  
        
        Console.WriteLine();
        
        Console.WriteLine("Array in reverse order: ");
        //Loop through the array in reverse order
        for (int i = arr.Length-1; i >= 0; i--) { 
            Console.Write(arr[i] + " "); 
        }  
    }
}

Output:

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

PHP

<!DOCTYPE html>
<html>
<body>
<?php
//Initialize array 
$arr = array(1, 2, 3, 4, 5); 
 
print("Original array: <br>");
for ($i = 0; $i < count($arr); $i++) { 
    print($arr[$i] . " "); 
}  
 
print("<br>");
 
print("Array in reverse order: <br>");
//Loop through the array in reverse order
for ($i = count($arr)-1; $i >= 0; $i--) { 
    print($arr[$i] . " "); ; 
}    
?>
</body>
</html>

Output:

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

Next Topic#




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf