Advertisement
Masovski

[C# Basics][Loops-HW] 6. Calculate N!/K!

Mar 29th, 2014
301
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.  
  3. class CalculateNFacOnKFac
  4. {
  5.     static void Main()
  6.     {
  7.         Console.Write("Enter n: ");
  8.         double n = double.Parse(Console.ReadLine());
  9.         Console.Write("Enter k: ");
  10.         double k = double.Parse(Console.ReadLine());
  11.         bool inRange = n < 100 && k > 1 && n > k;
  12.  
  13.         if (inRange)
  14.         {
  15.             double nFactorial = 1;
  16.             double kFactorial = 1;
  17.             double result = 0;
  18.  
  19.             for (int i = 1; i <= n; i++)
  20.             {
  21.                 nFactorial *= i;
  22.  
  23.                 if (i <= k)
  24.                 {
  25.                     kFactorial *= i;
  26.                 }
  27.             }
  28.  
  29.             result = nFactorial / kFactorial;
  30.             Console.WriteLine("{0}", result);
  31.         }
  32.         else
  33.         {
  34.             Console.WriteLine("Invalid input. Correct input --> 1 < k < n < 100");
  35.         }
  36.     }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement