For the problem Small Factorials I wrote the following C Code. It is working on my PC. I cross-checked the answer for 100! with a Python Script so answer is correct. I don’t have any idea why this is giving wrong answer.
I am using an integer array to store the digits. 4 digits at each array index. Lowest 4 digits at ans[0], next 4 at ans1 and so on.
It seems that your code is actually correct in terms of its logic, but, I suggest you retry writing it using the trick of storing only one digit at each array position!
The main reason for this is that it’s simple for you to do the arithmetics if you manipulate a single digit at a time, also due to carry.
@vjozic…factorials achieve large values…double can store these values…BUT there is a loss of precision…so u need to use an array of integers to store such high numbers…u can store few digits of the factorial in each cell like…
1!=|1|
2!=|2|
3!=|6|
4!=|2|4|
5!=|1|2|0|
it is like making your own datatype…hope this helps…
I don’t think so… There might be something you are not taking into account, some special case, possibly related to number of zeroes or some extra newline… That’s why doing the arithmetics with 1 digit at a time is safer, as it gives you more control