I was coding on LeetCode and made a program to reverse the order of a number. For example, 123 becomes 321, etc.
I assumed that the integer variable "ans" would be automatically initialized to zero by default. However, when I use the uninitialized variable ans later in the program, I get an error.
Note : this program works perfectly fine if I do initialize ans to zero. But I was just wondering why not initializing ans would get me an error.
class Solution {
public int reverse(int x) {
int temp;
int ans; //uninitialized!
while(x != 0)
{
temp = x % 10;
ans *= 10; //causes an error!
ans += temp;
x /= 10;
}
return ans;
}
}