Lm35 values capped at 31degree C

Wonderful when it works and to get such a helpful answer. Far be it for me to comment, but even I can't see anything in your code that declares what sort of number to use - int, long, unsigned int, unsigned long.
Numbers increasing and decreasing over a critical value do indicate an overflow. The critical numbers are 2^8, 2^16 and 2^32. If declaring long fixed it, then that's the answer. Don't forget the ranges of these numbers and the difference between signed and unsigned - signed have half the positive or negative range of unsigned numbers. Even the biggest number 2^32 unsigned eventually overflows. Counting milliseconds, you get the overflow at about 51-days. Eventually you end up having to split the big numbers into byte-sized chunks like high and low bytes.
I wish all problems were that straightforward.