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

21 May, 2021
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++
Java
Python3
C#
PHP
Javascript
// C program to copy the contents// of one array into another// in the reverse order #include <stdio.h> // Function to print the arrayvoid printArray(int arr[], int len){    int i;    for (i = 0; i < len; i++) {        printf("%d ", arr[i]);    }} // Driver codeint 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;}
// Java program to copy the contents// of one array into another// in the reverse orderclass GFG { // Function to print the arraystatic void printArray(int arr[], int len){    int i;    for (i = 0; i < len; i++)    {        System.out.printf("%d ", arr[i]);    }} // Driver codepublic 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
# Python3 program to copy the contents of one# array into another in the reverse orderimport math as mt # Function to print the arraydef printArray(arr, Len):     for i in range(Len):        print(arr[i], end = " ")     # Driver codeoriginal_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 Orderfor i in range(Len):    copied_arr[i] = original_arr[Len - i - 1] # Print the original_arrprint("Original array: ", end = "")printArray(original_arr, Len) # Print the copied arrayprint("\nResultant array: ", end = "")printArray(copied_arr, Len) # This code is contributed by# Mohit kumar 29
// C# program to copy the contents// of one array into another// in the reverse orderusing System;class GFG{ // Function to print the arraystatic void printArray(int []arr, int len){    int i;    for (i = 0; i < len; i++)    {        Console.Write(arr[i]);    }} // Driver codepublic 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
<?php// PHP program to copy the contents// of one array into another// in the reverse order // Function to print the arrayfunction 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 Orderfor ($i = 0; $i < $len; $i++){    $copied_arr[$i] = $original_arr[$len - $i - 1];} // Print the original_arrecho "Original array: ";printArray($original_arr, $len); // Print the copied arrayecho "\nResultant array: ";printArray($copied_arr, $len); // This code is contributed by Ryuga?>
<script> // JavaScript program to copy the contents// of one array into another// in the reverse order // Function to print the arrayfunction printArray(arr, len){    var i;    for (i = 0; i < len; i++) {        document.write( arr[i] + " ");    }} // Driver codevar original_arr = [1, 2, 3, 4, 5];var len = original_arr.length;var copied_arr = Array(len), i, j; // Copy the elements of the array// in the copied_arr in Reverse Orderfor (i = 0; i < len; i++) {    copied_arr[i] = original_arr[len - i - 1];}// Print the original_arrdocument.write("Original array: ");printArray(original_arr, len); // Print the copied arraydocument.write("<br>Resultant array: ");printArray(copied_arr, len);  </script>
Original array: 1 2 3 4 5 
Resultant array: 5 4 3 2 1
 
Time Complexity: O(len)
Auxiliary Space: O(len)
princiraj1992
mohit kumar 29
Rajput-Ji
ankthon
nidhi_biet
subhammahato348
famously
Arrays
Reverse
C Language
C Programs
Arrays
Reverse
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Exception Handling in C++
TCP Server-Client implementation in C
'this' pointer in C++
Input-output system calls in C | Create, Open, Close, Read, Write
Multithreading in C
Strings in C
UDP Server-Client implementation in C
C Program to read contents of Whole File
Header files in C/C++ and its uses
How to return multiple values from a function in C or C++?