In this tutorial you will learn about the C++ Programs to Reverse Array Elements and its application with practical example.
In this tutorial, we will learn to create a c++ program to reverse an array entered by user at run-time using c++ programming.
Before starting with this tutorial we assume that you are best aware of the following C++ programming topics:
- Looping statements.
- Basic input/output.
- Basic c++ programming
- For Loop.
What is array?
Programs to Reverse Array Elements.
In this program , elements of an Array will be reverse . First of all user will be prompted to enter array elements and will we reverse the elements of array.
using namespace std;
cout<< "enter size of array"; // taking the size of array
cin >> num;
cout<<"enter elements"; // getting values
for(i = 0; i < num; i++)
cin >> arr[i];
cout << "Reversed array is:\n"; // reversing array.
for(i = num-1; i >= 0; i--)
cout << arr[i] << " ";
cout << endl;
In the above program, we have first declared and initialized a set variables required in the program.
- num = it will hold size of array.
- arr= it will hold multiple elements.
- i = for iteration of loop.
In our program, we print the elements of the array in reverse order.
i.e the last element of array should be displayed first,
followed by second last element and so on.First of all we take a number as input from user to entered number store it in an array.Declare array of size equal to inputted num .
And now Input the array elements into the variable arr.
And after that traverse the array in reverse from last to first and print the array element in reverse order that’s it.