Description
The reverse of array means the elements we entered first will come last. example: if we have array, arr[] = {1,2,3} then reverse of array will be {3,2,1} To reverse the array, we start traversing the array from end to start and print the corresponding elements.
C/C++
/*C program to print reverse of the array*/ //Save it as ReverseArray.c #include<stdio.h> int main(){ int i,n; printf("Enter the size of array : "); scanf("%d",&n); int arr[n]; printf("Enter the array elements : "); for(i=0;i<n;i++) { scanf("%d",&arr[i]); } printf("The Entered elements are : "); for(i=0;i<n;i++) { printf("%d ",arr[i]); } printf("\nThe reverse of array is : "); for(i=n-1;i >= 0;i--) { printf("%d ",arr[i]); } return 0; }
Input: Enter the size of array : 5 Enter the array elements : 2 3 5 6 1 Output: The Entered elements are : 2 3 5 6 1 The reverse of array is : 1 6 5 3 2
Java
/*Java program to print reverse of the array*/ //Save it as ReverseArray.java public class ReverseArray { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("Enter the size of array : "); int n = scanner.nextInt(); //Declaring the array int arr[] = new int[n]; System.out.println("Enter the array elements : "); for(int i=0;i<n;i++) { arr[i] = scanner.nextInt(); } System.out.println("The Entered elements are : "); for(int i=0;i<n;i++) { System.out.print(arr[i] + " "); } System.out.println(); System.out.println("The reverse of array is : "); for(int i=n-1;i >= 0;i--) { System.out.print(arr[i] + " "); } } }
Input: Enter the size of array : 5 Enter the array elements : 2 3 5 6 1 Output: The Entered elements are : 2 3 5 6 1 The reverse of array is : 1 6 5 3 2
Related Programs
1) Program to Reverse an Array using Recursion2) Program to reverse a number
3) Program to Read and Display entered numbers using an Array
4) Find the Average of Elements of Array
5) Program to form a number using entered digits
6) Program to merge two sorted arrays
7) Program to read and display 2D array
8) write a Program to find index of two array elements whose sum is equal to given value
9) Program to make pair of elements alternatively
10) Program to Print How Many Numbers Smaller than Current Number
11) Remove Duplicate Elements From Array