Krissy

H6_Loops_T4_NKFactorial

Dec 8th, 2012
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.03 KB | None | 0 0
  1. //Write a program that calculates N!/K! for given N and K (1<K<N).
  2. using System;
  3. using System.Numerics;
  4.  
  5.  
  6. class NKfactorial
  7. {
  8.     static void Main()
  9.     {
  10.         Console.WriteLine("Please enter number k, which has to be integer bigger than 1:");
  11.         int k = int.Parse(Console.ReadLine());
  12.         Console.WriteLine("Please enter number n, which has to be integer bigger than k:");
  13.         int n = int.Parse(Console.ReadLine());
  14.  
  15.         BigInteger productNKFactorial = 1;
  16.         if (k > 1 && n > k)//Checks whether input is correct.
  17.         {
  18.             for (int i = n; i > k; i--)
  19.                 //I do not calculate separately N! and K!, because K!
  20.                 //is contained in N! and therefore the common parts are not needed to be calculated.
  21.             {
  22.                 productNKFactorial = productNKFactorial * i;
  23.             }
  24.             Console.WriteLine("The result is: {0}", productNKFactorial);
  25.         }
  26.  
  27.         else
  28.         {
  29.             Console.WriteLine("Wrong input!");
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment