ntodorova

05_CalculateNFaktMultiplyKFact

Nov 14th, 2012
898
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.91 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. /*
  5.  * 5. Write a program that calculates N!*K! / (K-N)! for given N and K (1 < N < K).
  6.  */
  7. class CalculateNFaktMultiplyKFact
  8. {
  9.     static void Main()
  10.     {
  11.         uint n;
  12.         uint k;
  13.         BigInteger result = 1;
  14.  
  15.         Console.Write("N = ");
  16.         string strN = Console.ReadLine();
  17.  
  18.         Console.Write("K = ");
  19.         string strK = Console.ReadLine();
  20.  
  21.         if (!uint.TryParse(strN, out n))
  22.         {
  23.             Console.WriteLine("Invalid number: {0}", strN);
  24.         }
  25.         else if (!uint.TryParse(strK, out k))
  26.         {
  27.             Console.WriteLine("Invalid number: {0}", strK);
  28.         }
  29.         else
  30.         {
  31.             if (k > n && n > 1)
  32.             {
  33.                 for (uint i = 1; i <= k; i++)
  34.                 {
  35.                     if (i <= n)
  36.                     {
  37.                         result *= i;
  38.                     }
  39.  
  40.                     if (i >= (k - n + 1))
  41.                     {
  42.                         result *= i;
  43.                     }
  44.                 }
  45.  
  46.                 Console.WriteLine("The result is: {0}", result);
  47.             }
  48.             else
  49.             {
  50.                 Console.WriteLine("Wrong numbers entered!");
  51.             }
  52.         }
  53.     }
  54. }
Advertisement
Add Comment
Please, Sign In to add comment