For loop confused as to which value to consider

I have this loop here:

while(n != 0)
    {
        ld = n % 10;
        System.out.println(ld);

        for ( i=1; i <= ld; i++)
        {
            f = f * i;
        }

        System.out.println(f);

        n /= 10;
    }

Let us consider a number, say 123. Now, this loop calculates the factorial of 3 accurately, but when it restarts to calculate the factorial of 2, the for loop gets confused as to which value to take for the variable ld. Is there any solution to this?


Click here view more answers http://ift.tt/1QXwsQi