C++ Program to Reverse elements of an array

Learn how to reverse an array

Today, we will learn how 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.
For Example:-
arr{1,2,3,4,5} is the array input by the user and the reverse order of it should be arr {5,4,3,2,1}

C++ Program to Reverse elements of an array

Algorithm:-

  1. INITIALIZE a[100] and b[100]
  2. Enter size of an array.
  3. PRINT “Array Elements:”
  4. REPEAT STEP 5 and STEP 6 UNTIL i=0
  5. PRINT arr[p]
  6. p=p+1
  7. PRINT new line
  8. PRINT “Array in reverse order”
  9. SET p=length-1. REPEAT STEP 10 and STEP 11 UNTIL p>=0
  10. PRINT a[p]
  11. p=p-1

C++ Code:-


// C++ code to Reverse of an array
    #include<iostream>
    using namespace std;
    int main()
    {
        int npqa[100], b[100];
        cout<<“Enter the number of elements in an array:\n;      //input size of array
        cin>>n;

        cout<<“Enter the elements of an array:\n;       //print elements of an array
        for (p = 0p < n ; p++)
        cin>>a[p];

        for (p = n – 1p = 0p >= 0p–, q++)
        b[q] = a[p];

        for (p = 0p < np++)     //Reverse of digit 
        a[p] = b[p];
        cout<<“Reverse of an array is:\n;
        
        for (p = 0p < np++)
        cout<<a[p]<<\n;       //print Reverse of an array

        return 0;
    }
    
    Output:
    Enter the number of elements in an array:
    5
    Enter the elements of an array:
    12
    23
    30
    46
    55
    Reverse of an array is:
    55
    46
    30
    23
    12

Important Codes related to Arrays

  • Find Smallest Element in an Array : C | C++ | Java | Python
  • Find Second Smallest Element in an Array : C | C++ | Java | Python
  • Find Largest element in an array : C | C++ | Java | Python
  • Find the Smallest and largest element in an array : C | C++ | Java | Python
  • Calculate the sum of elements in an array : C | C++ | Java | Python
  • Reverse an Array : C | C++ | Java | Python
  • Sort first half in ascending order and second half in descending : C | C++ | Java | Python
  • Sort the elements of an array : C | C++ | Java | Python
  • Finding the frequency of elements in an array : C | C++ | Java | Python
  • Finding the Longest Palindrome in an Array : C | C++ | Java| Python
  • Counting Distinct Elements in an Array : C | C++ | Java| Python
  • Finding  Repeating elements in an Array : C | C++ | Java | Python
  • Finding Non Repeating elements in an Array : C | C++ | Java | Python
  • Removing Duplicate elements from an array : C | C++ | Java
  • Finding Minimum scalar product of two vectors : C | C++ | Java | Python
  • Finding Maximum scalar product of two vectors in an array : C | C++ | Java | Python
  • Counting the number of even and odd elements in an array : C | C++ | Java
  • Find all Symmetric pairs in an array : C | C++ | Java
  • Find maximum product sub-array in a given array : C | C++ | Java
  • Finding Arrays are disjoint or not : C | C++ | Java
  • Determine Array is a subset of another array or not : C | C++ | Java
  • Determine can all numbers of an array be made equal : C | C++ | Java
  • Finding Minimum sum of absolute difference of given array : C | C++ | Java
  • Sorting elements of an array by frequency : C | C++ | Java
  • Sort an array according to the order defined by another array : C | C++ | Java
  • Replace each element of the array by its rank in the array : C | C++ | Java
  • Finding equilibrium index of an array : C | C++ | Java| Python
  • Rotation of elements of array- left and right : C | C++ | Java| Python
  • Block swap algorithm for array rotation : C | C++ | Java| Python
  • Juggling algorithm for array rotation : C | C++ | Java | Python
  • Finding Circular rotation of an array by K positions : C | C++ | Java | Python

You can find the below Programs in all languages

array