Program to copy the contents of one array into another in the reverse order

Given an array, the task is to copy these array elements into another array in reverse array.

Examples:

Input: array: 1 2 3 4 5 
Output: 5 4 3 2 1 

Input: array: 10 20 30 40 50 
Output: 50 40 30 20 10 

Let len be the length of original array. We copy every element original_arr[i] to copy_arr[n-i-1] to get reverse in copy_arr[].



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C program to copy the contents
// of one array into another
// in the reverse order
  
#include <stdio.h>
  
// Function to print the array
void printArray(int arr[], int len)
{
    int i;
    for (i = 0; i < len; i++) {
        printf("%d ", arr[i]);
    }
}
  
// Driver code
int main()
{
    int original_arr[] = {1, 2, 3, 4, 5};
    int len = sizeof(original_arr)/sizeof(original_arr[0]);
  
    int copied_arr[len], i, j;
     
    // Copy the elements of the array
    // in the copied_arr in Reverse Order
    for (i = 0; i < len; i++) {
        copied_arr[i] = original_arr[len - i - 1];
    }
  
    // Print the original_arr
    printf("\nOriginal array: ");
    printArray(original_arr, len);
  
    // Print the copied array
    printf("\nResultant array: ");
    printArray(copied_arr, len);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to copy the contents
// of one array into another
// in the reverse order
class GFG {
  
// Function to print the array
static void printArray(int arr[], int len)
{
    int i;
    for (i = 0; i < len; i++)
    {
        System.out.printf("%d ", arr[i]);
    }
}
  
// Driver code
public static void main(String[] args) 
{
    int original_arr[] = {1, 2, 3, 4, 5};
    int len = original_arr.length;
    int copied_arr[] = new int[len], i, j;
      
    // Copy the elements of the array
    // in the copied_arr in Reverse Order
    for (i = 0; i < len; i++)
    {
        copied_arr[i] = original_arr[len - i - 1];
    }
  
    // Print the original_arr
    System.out.printf("\nOriginal array: ");
    printArray(original_arr, len);
  
    // Print the copied array
    System.out.printf("\nResultant array: ");
    printArray(copied_arr, len);
    }
}
  
// This code is contributed by 
// PrinciRaj1992 

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to copy the contents of one
# array into another in the reverse order
import math as mt
  
# Function to print the array
def printArray(arr, Len):
  
    for i in range(Len):
        print(arr[i], end = " ")
      
# Driver code
original_arr = [1, 2, 3, 4, 5]
Len = len(original_arr)
  
copied_arr = [0 for i in range(Len)]
  
# Copy the elements of the array
# in the copied_arr in Reverse Order
for i in range(Len): 
    copied_arr[i] = original_arr[Len - i - 1]
  
# Print the original_arr
print("Original array: ", end = "")
printArray(original_arr, Len)
  
# Print the copied array
print("\nResultant array: ", end = "")
printArray(copied_arr, Len)
  
# This code is contributed by
# Mohit kumar 29

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to copy the contents 
// of one array into another 
// in the reverse order
using System;
class GFG 
  
// Function to print the array 
static void printArray(int []arr, int len) 
    int i; 
    for (i = 0; i < len; i++) 
    
        Console.Write(arr[i]); 
    
  
// Driver code 
public static void Main() 
    int []original_arr = {1, 2, 3, 4, 5}; 
    int len = original_arr.Length; 
    int []copied_arr = new int[len];
    int i; 
      
    // Copy the elements of the array 
    // in the copied_arr in Reverse Order 
    for (i = 0; i < len; i++) 
    
        copied_arr[i] = original_arr[len - i - 1]; 
    
  
    // Print the original_arr 
    Console.Write("\nOriginal array: "); 
    printArray(original_arr, len); 
  
    // Print the copied array 
    Console.Write("\nResultant array: "); 
    printArray(copied_arr, len); 
    
  
// This code is contributed by Rajput-Ji

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to copy the contents 
// of one array into another 
// in the reverse order 
  
// Function to print the array 
function printArray($arr, $len
    for ($i = 0; $i < $len; $i++)
    
        echo $arr[$i], " "
    
  
// Driver code 
$original_arr = array(1, 2, 3, 4, 5); 
$len = sizeof($original_arr);
  
$copied_arr = array();
  
// Copy the elements of the array 
// in the copied_arr in Reverse Order 
for ($i = 0; $i < $len; $i++) 
    $copied_arr[$i] = $original_arr[$len - $i - 1]; 
  
// Print the original_arr 
echo "Original array: "
printArray($original_arr, $len);
  
// Print the copied array 
echo "\nResultant array: "
printArray($copied_arr, $len); 
  
// This code is contributed by Ryuga
?>

chevron_right


Output:

Original array: 1 2 3 4 5 
Resultant array: 5 4 3 2 1


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.