int reverse(int x) {
int digit;
long rnum = 0;
int n = x;
while(n != 0)
{
digit = n % 10;
rnum = rnum*10 + digit;
n = n/10;
// Checking the overflow
if((rnum > INT_MAX)  (rnum < INT_MIN))
return 0;
}
return rnum;
}
Solution in C


@camelsa This is a running code. Could you please tell me what errors are you getting ?

@fakke These are predefined in C under <limits.h>. You could also refer to the below link for other data types upper and lower limits, if interested.
https://en.wikibooks.org/wiki/C_Programming/limits.h

@a0412comwg I have put the check for overflow of rnum. Do you suspect anything else. Let me know.


@NealC "rnum = rnum*10 + digit;" maybe overflow, which means your "Checking the overflow" is not credible;

#include <stdio.h>
#include <limits.h>
int reverse(long);
int main()
{
printf("%d ", reverse(123467900111));
}int reverse(long value)
{
int flag = 0;
int digit = 0;
int results = 0;if((value >= INT_MIN) && (value <= INT_MAX)) return 0; if (value < 0) { flag++; value = ~value + 1; } while (value > 0) { digit = value % 10; results = (results * 10) + digit; value = value / 10; if (results < INT_MIN) return 0; } if (flag > 0) return results * 1; else return results;
}
We need a bigger input size to even try checking overflow or string for that matter. here is a quick fix.

You can speed up the function by putting a check for out of range (32 bit Int) or equal to 0. Also, I noticed that no solution explicitly returns an integer even though the function defines the return of an integer (Actually Parsike does). Therefore, casting the long as an int in the return statement is a good solution.
int reverse(int num) { // Test for number out of range or zero if ((num > 2147483647)  (num < 2147483648)  (num == 0)) { return 0; } long reversed = 0; while (num != 0) { reversed = (reversed * 10) + (num % 10); num /= 10; } // Is reversed number bigger than 32 bit int? if ((reversed > 2147483647)  (reversed < 2147483648)) { return 0; } return (int)reversed; }