Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace FactorialWithArray
- {
- class FactWithArr
- {
- static void Main(string[] args)
- {
- int n = int.Parse(Console.ReadLine());
- int[] result = factorialOf(n);
- foreach (int c in result)
- {
- Console.Write(c);
- }
- Console.WriteLine();
- }
- private static int[] factorialOf(int number)
- {
- int[] result = new int[number * 2]; //Too big. Will remove NULL values later
- int total = 0;
- int rem = 0;
- int count = 0;
- int index = result.Length - 1;
- result[index] = 1;
- for (count = 2; count <= number; count++)
- {
- while (index > 0)
- {
- total = result[index] * count + rem;
- rem = 0;
- if (total > 9)
- {
- result[index] = total % 10;
- rem = total / 10;
- }
- else
- {
- result[index] = total;
- }
- index--;
- }
- total = 0;
- rem = 0;
- index = result.Length - 1;
- }
- return removeNullElementsFrom(result); //Removing NULL values. Gotta look presentable
- }
- private static int[] removeNullElementsFrom(int[] array)
- {
- int count = 0;
- for (int i = 0; i < array.Length; i++)
- {
- if (array[i] == 0)
- {
- count++;
- continue;
- }
- break;
- }
- if (count > 0)
- {
- int size = array.Length - count;
- int[] tmpArr = new int[size];
- Array.Copy(array, count, tmpArr, 0, size);
- array = tmpArr;
- }
- return array;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement