Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- /*
- Problem 11. * Trailing Zeroes in N!
- Write a program that calculates with how many zeroes the factorial of a given number n has at its end.
- Your program should work well for very big numbers, e.g. n=100000. Examples:
- n trailing zeroes of n! explaination
- 10 2 3628800
- 20 4 2432902008176640000
- 100000 24999 think why
- */
- class TrailingZeroesInNFactorial
- {
- static void Main()
- {
- // the formula for finding trailing zeros in factorial is:
- // if n > 5 zeroes = n /5
- // if n > 5^2 zeroes = (n / 5^2) + (n / 5)
- // if n > 5^3 zeroes = (n / 5^3 )+ (n / 5^2) + (n / 5)
- // if n > 5^4 zeroes = (n / 5^4 )+ (n / 5^3) + ....
- Console.Write("n = ");
- int n = int.Parse(Console.ReadLine());
- int zeroCounter = 0;
- int divide;
- int power = 0;
- while (true)
- {
- divide = n / (int)Math.Pow(5, power);
- if (divide > 0)
- {
- zeroCounter += divide;
- }
- else
- {
- break;
- }
- power++;
- }
- Console.WriteLine(zeroCounter);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement