











Program Of Reversing Array Using C++
Reverse Array
Array is defined as a contiguous block of memory which allocates similar type of data. In Array indexing starts with 0 and ends with -1.
In reverse array , we traverse elements from end to start.


How to Reverse An Array in C++ :
In general, Array can be reversed without using in-built method is loop. By using loop we can reverse the array easily.
Below are the two techniques to reverse the array using for loop :
Method 1 :
In the below program, we traverse the string using for loop from back and print the particular character of the string to get the reverse string.
#include<iostream> using namespace std; int main () { int arr[50], num, temp, i, j; cout << " Please, enter the total no. you want to enter = "; cin >> num; for (i = 0; i < num; i++) { cout << " Enter the element " << i + 1 << ": "; cin >> arr[i]; } for (i = 0, j = num - 1; i < num / 2; i++, j--) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } cout << "\n Reverse all elements of the array = " << endl; for (i = 0; i < num; i++) { cout << arr[i] << " "; } return 0; }
Output :
Please, enter the total no. you want to enter = 3 Enter the element 1: 67 Enter the element 2: 89 Enter the element 3: 34 Reverse all elements of the array = 34 89 67
In the below program, we traverse the string using for loop from start and print the particular character of the string to get the reverse string by using [n-i] to get the particular character from back
Method 2:
#include<iostream> #include<algorithm> using namespace std; void disp (int arr1[], int num) { int i; for (i = 0; i < num; i++) { cout << arr1[i] << " "; } } void reverse (int arr1[], int num) { reverse (arr1, arr1 + num); } int main () { int arr1[] = { 1, 2, 3, 4, 5, 6, 7 }; int num = sizeof (arr1) / sizeof (arr1[0]); reverse (arr1, num); disp (arr1, num); return 0; }
Output :
7 6 5 4 3 2 1
For more related content : Click-Here
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment