Advertisement
Krissy

H6_Loops_T5_FactorialNK

Dec 8th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.28 KB | None | 0 0
  1. //Write a program that calculates N!*K! / (K-N)! for given N and K (1<N<K).
  2.  
  3. using System;
  4. using System.Numerics;
  5.  
  6. class NKfactorialDividedKMinusNFactorial
  7. {
  8.     static void Main()
  9.     {
  10.         Console.WriteLine("Please enter number n, which has to be integer bigger than 1:");
  11.         int n = int.Parse(Console.ReadLine());
  12.         Console.WriteLine("Please enter number k, which has to be integer bigger than n:");
  13.         int k = int.Parse(Console.ReadLine());
  14.  
  15.         BigInteger productNFactorial = 1;
  16.         if (n > 1 && k > n)//Checks input data.
  17.         {
  18.             for (int i = n; i > 1; i--)
  19.             {
  20.                 productNFactorial = productNFactorial * i;//Calculates N!
  21.             }
  22.  
  23.  
  24.             BigInteger productKFactorialDividedKMunusNFactorial = 1;
  25.             for (int i = k; i > (k - n); i--)// Calculates K! / (K-N)!, but omits the common parts.
  26.             {
  27.                 productKFactorialDividedKMunusNFactorial = productKFactorialDividedKMunusNFactorial * i;
  28.             }
  29.             BigInteger finalResult = productKFactorialDividedKMunusNFactorial * productNFactorial;
  30.             Console.WriteLine("The result is: {0}", finalResult);
  31.         }
  32.         else
  33.         {
  34.             Console.WriteLine("Wrong input!");
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement