No it doesn’t, look at this case: http://ideone.com/z3hb4mYour approach is too naive. Like in most other SPOJ problems, it is a good idea to create a naïve, often brute-force approach. Once you have this kind of code, you can try to optimize it. Closely look what the problem statement asks you for (here: just the last digit), and watch out for patterns, think how to get the desired result in a more efficient way.