(Reverse Digits) Write a function that takes an integer value and returns the number with its digits reversed. For example, given the number 7631, the function should return 1367.
Solution:
Solution:
#include <iostream> using std::cout; using std::cin; using std::endl; #include <cmath> int reverse(int); int main() { int i; cout << "Enter integer: "; cin >> i; cout << "Reversed digits: " << reverse(i); return 0; } // reverse function int reverse (int i) { int digitsCount = 0; for(int digits = 1; digits <= i; digits *= 10) { digitsCount++; } int x = 0; int a = digitsCount; for(int j = 1; j <= digitsCount; j++) { a--; x += (i % 10) * pow(10, a); i = i / 10; } return x; }
Post A Comment:
0 comments: