Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace TrailingZeroesInNFactorial
- {
- using System;
- /// <summary>
- /// Problem 18.* 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
- /// </summary>
- public class TrailingZeroesInNFactorial
- {
- public static void Main()
- {
- Console.Write("Please enter an integer number n: ");
- int n;
- if (!int.TryParse(Console.ReadLine(), out n))
- {
- Console.WriteLine("Input was not in the correct format.");
- return;
- }
- Console.Write("{0}! --> ", n);
- // fast way - find the number of factors 5 in n! and since there are at least as much factors 2, this equivalents to the number of factors 10, each of which gives one more zero
- int zerosCount = 0;
- while (n > 0)
- {
- n = n / 5;
- zerosCount += n;
- }
- Console.WriteLine("trailing zeroes : {0}", zerosCount);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement