C++ examples for Data Structure:Algorithm
Reverse the digits of a given int
#include <iostream> int reverseDigits(int); int main(int argc, const char *argv[]) { int n;/*from w ww. j a v a 2s . co m*/ std::cout << "Enter a number to be reversed: "; std::cin >> n; std::cout << "\n" << reverseDigits(n) << std::endl; return 0; } // returns a reversed integer int reverseDigits(int n) { int tmp = 0; int digits = 1; // get number of digits for (int d = 1; d <= n; d *= 10) { digits *= 10; } while (n != 0) { // reduce digits before using (avoid multiplying final value) digits /= 10; tmp += n % 10 * digits; n /= 10; } return tmp; }