Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * * Write a program that calculates for given N how many trailing zeros present at the end of the number N!. Examples:
- N = 10 N! = 3628800 2
- N = 20 N! = 2432902008176640000 4
- Does your program work for N = 50 000?
- Hint: The trailing zeros in N! are equal to the number of its prime divisors of value 5. Think why!
- */
- using System;
- using System.Numerics;
- class ZerosAtTheEndOfFactorialN
- {
- static void Main()
- {
- BigInteger numberN;
- BigInteger trailingZeros = 0;
- BigInteger divider = 5;
- string invalidInput = "Please enter a valid number greater than 0!" + Environment.NewLine;
- Console.WriteLine("Enter a value for N: ");
- while (!(BigInteger.TryParse(Console.ReadLine(), out numberN) && numberN > 0))
- {
- Console.WriteLine(invalidInput);
- Console.WriteLine("Enter a value for N: ");
- }
- for (int i = 5; i <= numberN*2; i = i * 5)
- {
- trailingZeros = trailingZeros + (numberN / i);
- }
- Console.WriteLine("There are {0} trailing zeros in factorial of number {1}!" + Environment.NewLine, trailingZeros, numberN);
- Main();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment