>Now since you have it as unsigned it creates problems by genrating random outputs.
There's nothing random about it. Unsigned underflow wraps around to the largest value for the type. The problem is that the loop is infinite because an unsigned integer cannot hold a value less than 0 whereas a plain integer, which is signed, can. The behavior is well defined, so the following loop is incorrect:
Code:for (unsigned i = n; i >= 0; i--)