Venciity

[ Telerik C#] LOOPS - EX 13

Feb 13th, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.38 KB | None | 0 0
  1. // 13. * Write a program that calculates for given N how many trailing zeros present at the end of the number N!.
  2. //  Examples:
  3. //  N = 10 ---> N! = 3628800 ---> 2
  4. //  N = 20 ---> N! = 2432902008176640000 ---> 4
  5. //  Does your program work for N = 50 000?
  6. //  Hint: The trailing zeros in N! are equal to the number of its prime divisors of value 5. Think why!
  7.  
  8. // 45:5=9, 9:5=1, значи 9+1=10 нули
  9.  
  10. using System;
  11. using System.Numerics;
  12.  
  13. namespace ex13
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             int n;
  20.             do
  21.             {
  22.                 Console.Write("Enter N: ");
  23.                 n = Int32.Parse(Console.ReadLine());
  24.             } while (!(n > 0));
  25.  
  26.             BigInteger nFact = 1;
  27.             for (int i = 1; i <= n; i++)
  28.             {
  29.                 nFact = nFact * i;
  30.             }
  31.             Console.WriteLine("{0}! = {1}",n,nFact);
  32.  
  33.             int zeroCounter = 0;
  34.             while (true)
  35.             {
  36.                 int result = n / 5;
  37.                 if (result != 0)
  38.                 {
  39.                     zeroCounter = zeroCounter + result;
  40.                     n = result;
  41.                 }
  42.                 else
  43.                 {
  44.                     Console.WriteLine("Trailing zeros count: {0}", zeroCounter);
  45.                     break;
  46.                 }
  47.             }
  48.         }
  49.     }
  50. }
Advertisement
Add Comment
Please, Sign In to add comment