How to get the last non zero number is the factorial of a given no. The given no. can have many digits even more than 100? I have to solve this problem in c++ using STL....
This is a common starter challenge in college and high school programming classes. While i'm not willing to code it for you, I can show you the methods you'll need to understand in order to approach the problem. The easiest way of doing this is by using a brute force method. Just keep testing the number to see if the remainder after dividing it with a number lower than it is zero. If so, it's a factor.
For example, If the number is 35 then pick a number just one lower than it that's not the number 1. Now divide the two numbers. 35/34 = a number with many decimal places after it, which means its not one of the factors of the number. However, eventually, you'll approach the number 7, and 35/7 = 5. This number has no decimal, meaning it IS a factor of 35, and also the largest non-zero factor of 35. Your algorithm can now stop, since it found the largest non-zero number.
Here's some pseudo-code:
int largestfactor(int number) {
int temp = number;
for(temp - 1; temp>=1; temp--) {
if (number % temp == 0) return temp
}
return temp
}
Something like that, with some bug tweaks will do it. The "%" sign is called a modulus. It's a standard c++ operator. Good luck, and remember to vote Fixya if you liked it.
The wikipedia article that helps you with better and more effecient algorithms can be found here.
Steven
144 views
Usually answered in minutes!
×