fork download
  1. #include <stdio.h>
  2.  
  3. int main() {
  4. int num;
  5. scanf("%d", &num);
  6.  
  7. int is_negative = 0;
  8.  
  9. if (num < 0) {
  10. is_negative = 1;
  11. num = -num; // Make it positive for reversal
  12. }
  13.  
  14. int r, rev = 0;
  15.  
  16. while (num != 0) {
  17. r = num % 10;
  18. rev = rev * 10 + r;
  19. num = num / 10;
  20. }
  21.  
  22. if (is_negative) {
  23. rev = -rev; // Add negative sign back
  24. }
  25.  
  26. printf("%d\n", rev);
  27. return 0;
  28. }
  29.  
Success #stdin #stdout 0s 5320KB
stdin
 0
-123
1000
stdout
0