BorislavBorisov

Loops Calculate N! / (K! * (N-K)!)

Sep 30th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.88 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3. class NumberOfCombinations
  4. {
  5.     static void Main()
  6.     {
  7.         int N = int.Parse(Console.ReadLine());
  8.         int K = int.Parse(Console.ReadLine());
  9.         Combinations(N, K);
  10.         Console.WriteLine(Combinations(N,K));
  11.     }
  12.     static BigInteger Combinations(int N, int K)
  13.     {
  14.         BigInteger factorialN = 1;
  15.         BigInteger factorialK = 1;
  16.         BigInteger factorialNK = 1;
  17.         BigInteger resFactorials = 0;
  18.         for (int i = 1; i <= N; i++)
  19.         {
  20.             factorialN = factorialN * i;
  21.             if(i <= K)
  22.             {
  23.                 factorialK = factorialK * i;
  24.             }
  25.             if(i <= N- K)
  26.             {
  27.                 factorialNK = factorialNK * i;
  28.             }
  29.         }
  30.         resFactorials = factorialK * factorialNK;
  31.         return factorialN / resFactorials;
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment