Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class Program
- {
- static void Main(string[] args)
- {
- int[] numbers = new int[] {100, 171, 250};
- foreach (int n in numbers)
- {
- Console.WriteLine("The factorial of {0} is:", n);
- ArrayFactorial(n);
- Console.WriteLine("\n");
- }
- }
- private static void ArrayFactorial(int n)
- {
- int[] a = new int[1000]; //array will have the capacity to store 200 digits.
- int i, j, temp, m, x;
- a[0] = 1; //initializes array with only 1 digit, the digit 1.
- m = 1; // initializes digit counter
- temp = 0; //Initializes carry variable to 0.
- for (i = 1; i <= n; i++)
- {
- for (j = 0; j < m; j++)
- {
- x = a[j] * i + temp; //x contains the digit by digit product
- a[j] = x % 10; //Contains the digit to store in position j
- temp = x / 10; //Contains the carry value that will be stored on later indexes
- }
- do //while loop that will store the carry value on array.
- {
- a[m] = temp % 10;
- temp = temp / 10;
- m++; // increments digit counter
- } while (temp > 0);
- }
- //printing answer
- for (i = m - 1; i >= 0; i--)
- {
- Console.Write(a[i]);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement