How to Reverse an Array in C++ using Functions

In this post, we will make a program to reverse an array in c++ using Functions which takes all the elements of the array using for loop from the user

In this post, we will make a program to reverse an array in c++ using Functions which takes all the elements of the array using for loop from the user, and then we will call our reverse function. We will also print the array in reverse order in C++ using for loop.

How to Reverse an Array in C++ using Functions

In this program, we will reverse an array in c++ using functions. We will swap the first number with the last one and so on.
#include<iostream>
void rev(int* a,int N);
using namespace std;
int main()
{   
    int arr[1000],n;
    cout<<"\n Enter number of Elements:";
    cin>>n;
    cout<<"\n Enter Array:";
    for (int i = 0; i < n; i++)
    {
        cin>>arr[i];
    }
    cout<<"\n Array:";
    for(int i=0; i<n;i++)
    cout<<arr[i]<<" ";
    rev(arr,n);
    return 0;
}
void rev(int* a,int N)
{
    int j, temp;
    j=N-1;
    for(int i=0;i<j;i++,j--)
    {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;   
    }
    cout<<"\n Reverse Array:";
    for(int i=0; i<N;i++)
    cout<<a[i]<<" ";
}
Output:

Enter the number of Elements:5
Array:11 22 33 44 55
Reversed Array:55 44 33 22 11

Reverse an array in c++ without using temp variable

We have not taken any temporary variable to reverse an array in this program. We will print the array in reverse order.
#include<iostream.h>
int main()
{

    int A[5],i,j,n;
    cout<<"\n Enter the size of array:";
    cin>>n;
    //n is size of array
    cout<<"\n Enter an array:";
    for(i=0;i<n;i++)
    cin>>A[i];
    cout<<"\n Reverse array:";
    for(j=n-1;j>=0;j--)
    cout<<A[j]<<" ";
}

Output:

Enter the size of the array: 5
Enter an array:1 2 3 4 5
Reverse array:5 4 3 2 1

Before going through this program, you should know about the following topics.
  • Basics of c++ programming language
  • for loop
  • Array initialization

 We did not reverse an array; we just printed the array in reverse order using "for loop" by iterating from the last element to the first element of the array.
How to reverse an array in c++

Algorithm to reverse an array in C++

  • Initialize an integer array.
  • Input the size of an array by the user.
  • Input elements of an array using for loop.
  • Iterate over the array in reverse order.

If you like this program or have any problem/difficulty with this program, then let me know by commenting below or contacting us.

About this program:

Siddharth Jha writes this program. It is one of the simplest ways to print the reverse of an array in c++.

On this site, we most simply upload the source code of different programs. Stay tuned to this site if you are a programmer or want to start coding from scratch. You can also subscribe to our newsletter to get updates about this blog.



2 comments

  1. i liked this
    1. Thanks
Please do not enter any spam link in the comment box.