Advertisement
soxa

calculatesZero

Nov 25th, 2013
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.87 KB | None | 0 0
  1. using System;
  2. //Write a program that calculates for given N how many trailing zeros present at the end of the number N!. Examples:
  3. //N = 10 --> N! = 3628800 --> 2
  4. //N = 20 ->> N! = 2432902008176640000  --> 4
  5. class CalculatesZero
  6. {
  7.     static void Main()
  8.     {
  9.         int fact = int.Parse(Console.ReadLine());
  10.         int zero = 0;
  11.         double number = 0; // save current value of number multiplied by five
  12.  
  13.         for (int i = 1; i <= fact; i++)
  14.         {
  15.             number = i;
  16.             while(number % 5 == 0)
  17.             {
  18.                 zero++;
  19.                 number /= 5;
  20.                 if (number % 5 != 0) // if the number is divisible by five, repead the operation, else
  21.                 {                    // break , and get another i !
  22.                     break;
  23.                 }
  24.             }
  25.         }
  26.         Console.WriteLine(zero);
  27.     }
  28. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement