reverse numbers in an array cpp code example
Example 1: reversing numbers in C++
#include <iostream>
#include <string>
using namespace std;
int reverse(int number);
int main() {
int x = 18475;
cout << "18475 reversed is " << reverse(18475) << endl;
}
int reverse(int number) {
int result = 0;
while (number > 0) {
result = result * 10 + number % 10;
number /= 10;
}
return result;
}
Example 2: c++ reverse array
int arr[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < size(arr); i++) {
int temp = arr[i];
arr[i] = arr[size(arr) - 1 - i];
arr[size(arr) - 1 - i] = temp;
}