Advertisement
dimipan80

6.11Loops_TrailingZeroesInNFactorials

Mar 25th, 2014
496
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.45 KB | None | 0 0
  1. // 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.
  2.  
  3. namespace _18.TrailingZeroesInN_Factorial
  4. {
  5.     using System;
  6.     using System.Numerics;
  7.  
  8.     public class TrailingZeroesInN_Factorial
  9.     {
  10.         public static void Main(string[] args)
  11.         {
  12.             checked
  13.             {
  14.                 int numN;
  15.                 do
  16.                 {
  17.                     Console.Write("Enter a positive Integer number: ");
  18.                 }
  19.                 while (!int.TryParse(Console.ReadLine(), out numN) || numN < 1);
  20.  
  21.                 // Just in case we'll calculate and N!:
  22.                 BigInteger factorialN = 1;
  23.                 for (int i = numN; i > 1; i--)
  24.                 {
  25.                     factorialN *= i;
  26.                 }
  27.                
  28.                 // Calculate count of Zeroes in N!, without calculating N! him self:
  29.                 int countZeroes = 0;
  30.                 int quotient = numN;
  31.                 do
  32.                 {
  33.                     quotient /= 5;
  34.                     countZeroes += quotient;
  35.                 }
  36.                 while (quotient > 1);                
  37.  
  38.                 Console.WriteLine("Factorial of number {0} is: {1}\n", numN, factorialN);
  39.                 Console.WriteLine("Trailing Zeroes in {0}! is: {1} !", numN, countZeroes);
  40.             }
  41.         }
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement