Reverse Number Program in C++ code example

Example 1: reversing numbers in C++

#include <iostream>
#include <string>

using namespace std;

int reverse(int number); // reverses the number's digits

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: print reverse number

void printReverse(int n) {
  if (n < 1) return;
  cout << n << endl;
  printReverse(n - 1);
}

printReverse(10);  /// 10 9 8 7 6 5 4 3 2 1