Why doesn't the GeeksForGeeks solution work


#1

I don’t understand why the GeeksForGeeks solution to this exact problem doesn’t work, would love if someone could explain it.

int reversDigits( int num)

{

// Handling negative numbers

bool negativeFlag = false ;

if (num < 0)

{

negativeFlag = true ;

num = -num ;

}

int prev_rev_num = 0, rev_num = 0;

while (num != 0)

{

int curr_digit = num%10;

rev_num = (rev_num*10) + curr_digit;

// checking if the reverse overflowed or not.

// The values of (rev_num - curr_digit)/10 and

// prev_rev_num must be same if there was no

// problem.

if ((rev_num - curr_digit)/10 != prev_rev_num)

{

printf ( "WARNING OVERFLOWED!!!\n" );

return 0;

}

prev_rev_num = rev_num;

num = num/10;

}

return (negativeFlag == true )? -rev_num : rev_num;

}


#2

I guess its better if u use INT_MIN and INT_MAX to check for overflow and also store the reverse number in long long int type