Advertisement
Fundamentalen

CombinationFormula

Mar 23rd, 2014
812
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. class CombinationFormula
  5. {
  6.     static void Main()
  7.     {
  8.         Console.Write("n = ");
  9.         int n = int.Parse(Console.ReadLine());
  10.         Console.Write("k = ");
  11.         int k = int.Parse(Console.ReadLine());
  12.  
  13.         if (n > 1 && k > 1 && n < 100 && k < 100)
  14.         {
  15.             BigInteger factN = 1;
  16.             BigInteger factK = 1;
  17.             BigInteger factNAndK = 1;
  18.             BigInteger result = 0;
  19.  
  20.             for (int i = 1; i <= n; i++)
  21.             {
  22.                 factN *= i;
  23.             }
  24.  
  25.             for (int j = 1; j <= k; j++)
  26.             {
  27.                 factK *= j;
  28.             }
  29.  
  30.             for (int z = 1; z <= n - k; z++)
  31.             {
  32.                 factNAndK *= z;
  33.             }
  34.  
  35.             result = factN / (factK * factNAndK);
  36.  
  37.             Console.WriteLine(result);
  38.         }
  39.         else
  40.         {
  41.             Console.WriteLine("Invalid input!");
  42.         }
  43.     }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement