kirililchev3

Factorial Trailing Zeroes

Jun 3rd, 2018
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.24 KB | None | 0 0
  1. namespace Factorial_Trailing_Zeroes
  2. {
  3. using System;
  4. using System.Numerics;
  5.  
  6. public class Factorial_Trailing_Zeroes
  7. {
  8. public static void Main(string[] args)
  9. {
  10. int number = int.Parse(Console.ReadLine());
  11. Console.WriteLine(TrailingZeroesInFactorial(number));
  12. }
  13.  
  14. private static int TrailingZeroesInFactorial(int number)
  15. {
  16. string stringFactorial = string.Empty + Factorial(number);
  17. int zeroesCount = 0;
  18. for (int i = stringFactorial.Length - 1; i >= 0; i--)
  19. {
  20. if (stringFactorial[i] == '0')
  21. {
  22. zeroesCount++;
  23. }
  24. else
  25. {
  26. break;
  27. }
  28. }
  29.  
  30. return zeroesCount;
  31. }
  32.  
  33. private static BigInteger Factorial(int number)
  34. {
  35. if (number == 0)
  36. {
  37. return 1;
  38. }
  39.  
  40. BigInteger factorial = number;
  41. for (int i = number - 1; i > 0; i--)
  42. {
  43. factorial = BigInteger.Multiply(factorial, i);
  44. }
  45.  
  46. return factorial;
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment