Advertisement
dimipan80

6.6Loops_CalculateFactorialsDivision

Mar 23rd, 2014
1,107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.55 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class CalculateFactorialsDivision
  5. {
  6.     static void Main ()
  7.     {
  8.         checked
  9.         {
  10.             Console.Write("Please, enter a whole number, bigger from 1 for K = ");
  11.             string numberStr = Console.ReadLine();
  12.             int numK = int.Parse(numberStr);
  13.             Console.Write("Enter other whole number, bigger from K for N = ");
  14.             numberStr = Console.ReadLine();
  15.             int numN = int.Parse(numberStr);
  16.             if (numN <= 1 || numK >= numN || numK < 1 || numN > 100 || numK >= 100)
  17.             {
  18.                 Console.WriteLine("Error - Invalid Input !!!");
  19.             }
  20.             else
  21.             {
  22.                 /* If looked deeply in factorial function formula, we will understand
  23.                  * why is not necessarily calculate N! and K! separately.
  24.                  * N!=N*(N-1)*(N-2)*...*1 and K!=K*(K-1)*(K-2)*...1;
  25.                  * If N>K K! is Subset of N!, that's mean:
  26.                  * N!= N*(N-1)*(N-2)*...*K*(K-1)*(K-2)*...*1;
  27.                  * Division of N! by K! means:
  28.                  * RESULT = N*(N-1)*(N-2)*...(K+1);
  29.                  * Example: What is 7! / 4! = 7 × 6 × 5 = 210;*/
  30.  
  31.                 BigInteger resultDivision = 1;
  32.                 for (int i = numN; i > numK; i--)
  33.                 {
  34.                     resultDivision *= i;
  35.                 }
  36.                 Console.WriteLine("The Result from Division of N! by K! is RESULT = " + resultDivision);
  37.             }
  38.             Console.ReadLine();
  39.         }
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement