Advertisement
soxa

nFact!

Jan 2nd, 2014
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.33 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. //Write a program to calculate n! for each n in the range [1..100]. Hint: Implement first a method that multiplies a number represented as array of digits by given integer number.
  4.  
  5. class CalculateN
  6. {
  7.     private static List<int> FactN(int index)
  8.     {
  9.         int[] mulIndex = new int[] { 1 };
  10.         List<int> currentNum = new List<int> { 1 };
  11.         List<int> number = new List<int>();
  12.         int remainder = 0;
  13.         int multiple = 0;
  14.  
  15.         for (int mulCount = 0; mulCount <= index; mulCount++)
  16.         {
  17.             mulIndex[0] = mulCount + 1;
  18.             number = new List<int>(currentNum);
  19.             remainder = 0;
  20.             currentNum.Clear();
  21.  
  22.             for (int numCount = 0; numCount < number.Count; numCount++)
  23.             {
  24.                 multiple = number[numCount] * mulIndex[0] + remainder;
  25.  
  26.                 if (multiple > 9)
  27.                 {
  28.                     int devider = multiple % 10;
  29.                     currentNum.Add(devider);
  30.                     remainder = multiple / 10;
  31.  
  32.                     if (numCount == number.Count - 1 && remainder > 0)
  33.                     {
  34.                         if (remainder > 9)
  35.                         {
  36.                             while (true)
  37.                             {
  38.                                 int devRemainder = remainder % 10;
  39.                                 currentNum.Add(devRemainder);
  40.                                 remainder /= 10;
  41.                                 if (remainder == 0)
  42.                                 {
  43.                                     break;
  44.                                 }
  45.                             }
  46.                         }
  47.                         else
  48.                         {
  49.                             currentNum.Add(remainder);
  50.                         }
  51.                     }
  52.                 }
  53.                 else
  54.                 {
  55.                     currentNum.Add(multiple);
  56.                     remainder = 0;
  57.                 }
  58.             }
  59.         }
  60.         return currentNum;
  61.     }
  62.  
  63.     static void Main()
  64.     {
  65.         int n = int.Parse(Console.ReadLine());
  66.         int[] factorial = FactN(n - 1).ToArray();
  67.  
  68.         for (int i = factorial.Length - 1; i >= 0; i--)
  69.         {
  70.             Console.Write(factorial[i]);
  71.         }
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement