fork download
  1. #include <iostream>
  2. #include <limits.h>
  3. using namespace std;
  4.  
  5. int reverse(int x) {
  6. //to accomodate overflowed reversed value if occured
  7. long revX = 0;
  8. while (x != 0) {
  9. revX = revX * 10 + x % 10;
  10. //overflow check
  11. if (revX > INT_MAX || revX < INT_MIN) {
  12. return 0;
  13. }
  14. x /= 10;
  15. }
  16. return (int) revX;
  17. }
  18.  
  19. int main() {
  20. int x = -321;
  21. cout << reverse(x);
  22. return 0;
  23. }
Success #stdin #stdout 0s 16064KB
stdin
Standard input is empty
stdout
-123