Advertisement
AnitaN

06.LoopsHomework/07.CalculateCombinatoric

Mar 29th, 2014
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.16 KB | None | 0 0
  1. //Problem 7.    Calculate N! / (K! * (N-K)!)
  2. //In combinatorics, the number of ways to choose k different members out of a group of n different elements (also known as the number of combinations) is calculated by the following formula:
  3. //For example, there are 2598960 ways to withdraw 5 cards out of a standard deck of 52 cards. Your task is to write a program that calculates n! / (k! * (n-k)!) for given n and k (1 < k < n < 100). Try to use only two loops.
  4.  
  5. using System;
  6. using System.Numerics;
  7.  
  8. class CalculateCombinatoric
  9. {
  10.     static BigInteger FactorialCalc(uint number)
  11.     {
  12.         BigInteger sum = 1;
  13.         for (int i = 1; i <= number; i++)
  14.         {
  15.             sum = sum * i;
  16.         }
  17.         return sum;
  18.     }
  19.  
  20.     static void Main()
  21.     {
  22.         Console.WriteLine("Calculate   N!/(K!*(N-K)!");
  23.         Console.Write("Enter N:");
  24.         uint n = uint.Parse(Console.ReadLine());
  25.         Console.Write("Enter K:");
  26.         uint k = uint.Parse(Console.ReadLine());
  27.         BigInteger result = FactorialCalc(n) / ((FactorialCalc(k) * FactorialCalc(n - k)));
  28.         Console.Write("The Result is:{0}",result);
  29.         Console.WriteLine();
  30.     }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement