Advertisement
Guest User

fact

a guest
Apr 3rd, 2015
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.18 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace FactorialWithArray
  8. {
  9. class FactWithArr
  10. {
  11. static void Main(string[] args)
  12. {
  13. int n = int.Parse(Console.ReadLine());
  14.  
  15. int[] result = factorialOf(n);
  16.  
  17. foreach (int c in result)
  18. {
  19. Console.Write(c);
  20. }
  21. Console.WriteLine();
  22. }
  23. private static int[] factorialOf(int number)
  24. {
  25. int[] result = new int[number * 2]; //Too big. Will remove NULL values later
  26. int total = 0;
  27. int rem = 0;
  28. int count = 0;
  29. int index = result.Length - 1;
  30. result[index] = 1;
  31. for (count = 2; count <= number; count++)
  32. {
  33. while (index > 0)
  34. {
  35. total = result[index] * count + rem;
  36. rem = 0;
  37. if (total > 9)
  38. {
  39. result[index] = total % 10;
  40. rem = total / 10;
  41. }
  42. else
  43. {
  44. result[index] = total;
  45. }
  46. index--;
  47. }
  48.  
  49. total = 0;
  50. rem = 0;
  51. index = result.Length - 1;
  52. }
  53.  
  54. return removeNullElementsFrom(result); //Removing NULL values. Gotta look presentable
  55. }
  56.  
  57. private static int[] removeNullElementsFrom(int[] array)
  58. {
  59. int count = 0;
  60. for (int i = 0; i < array.Length; i++)
  61. {
  62. if (array[i] == 0)
  63. {
  64. count++;
  65. continue;
  66. }
  67.  
  68. break;
  69. }
  70.  
  71. if (count > 0)
  72. {
  73. int size = array.Length - count;
  74. int[] tmpArr = new int[size];
  75. Array.Copy(array, count, tmpArr, 0, size);
  76. array = tmpArr;
  77. }
  78.  
  79. return array;
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement