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++
// 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
filter_none
Java
// 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
filter_none
Python3
# Python3 program to copy the contents of one # array into another in the reverse order import math as mt # Function to prthe 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
filter_none
C#
// 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
filter_none
PHP
<?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
filter_none
Output:
Original array: 1 2 3 4 5 Resultant array: 5 4 3 2 1
Recommended Posts:
- C program to copy contents of one file to another file
- Sort an array where a subarray of a sorted array is in reverse order
- C Program to Reverse Array of Strings
- C Program to print contents of file
- C Program to merge contents of two files into a third file
- Write a C program that displays contents of a given file like 'more' utility in Linux
- Reverse Level Order Traversal
- Print Fibonacci Series in reverse order
- Print words of a string in reverse order
- Print Doubly Linked list in Reverse Order
- Merge two sorted linked lists such that merged list is in reverse order
- C program to copy string without using strcpy() function
- First string from the given array whose reverse is also present in the same array
- Program to Reverse a String using Pointers
- C Program for Reverse a linked list
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.



