Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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.
- namespace _18.TrailingZeroesInN_Factorial
- {
- using System;
- using System.Numerics;
- public class TrailingZeroesInN_Factorial
- {
- public static void Main(string[] args)
- {
- checked
- {
- int numN;
- do
- {
- Console.Write("Enter a positive Integer number: ");
- }
- while (!int.TryParse(Console.ReadLine(), out numN) || numN < 1);
- // Just in case we'll calculate and N!:
- BigInteger factorialN = 1;
- for (int i = numN; i > 1; i--)
- {
- factorialN *= i;
- }
- // Calculate count of Zeroes in N!, without calculating N! him self:
- int countZeroes = 0;
- int quotient = numN;
- do
- {
- quotient /= 5;
- countZeroes += quotient;
- }
- while (quotient > 1);
- Console.WriteLine("Factorial of number {0} is: {1}\n", numN, factorialN);
- Console.WriteLine("Trailing Zeroes in {0}! is: {1} !", numN, countZeroes);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement