Venciity

[ Telerik C#] LOOPS - EX 5

Feb 9th, 2014
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.81 KB | None | 0 0
  1. //Write a program that calculates N!*K! / (K-N)! for given N and K (1<N<K).
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7.  
  8. namespace ex4
  9. {
  10.     class Program
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             int numberN, numberK;
  15.             string input;
  16.             int sum = 0;
  17.  
  18.             do
  19.             {
  20.                 Console.Write("Въведете число N: ");
  21.                 input = Console.ReadLine();
  22.             }
  23.             while (!(int.TryParse(input, out numberN) && (numberN > 0)));
  24.  
  25.             do
  26.             {
  27.                 Console.Write("Въведете число K: ");
  28.                 input = Console.ReadLine();
  29.             }
  30.             while (!(int.TryParse(input, out numberK) && (numberK < numberN)));
  31.  
  32.             int factorialN = 1;
  33.  
  34.             for (int counter = 1; counter <= numberN; counter++)
  35.             {
  36.                 factorialN *= counter;
  37.  
  38.             }
  39.  
  40.             Console.WriteLine("The factorial of {0} is {1}", numberN, factorialN);
  41.  
  42.             int factorialK = 1;
  43.  
  44.             for (int counter = 1; counter <= numberK; counter++)
  45.             {
  46.                 factorialK *= counter;
  47.  
  48.             }
  49.  
  50.             Console.WriteLine("The factorial of {0} is {1}", numberK, factorialK);
  51.  
  52.             int difference = numberN - numberK;
  53.             int factorialDifference = 1;
  54.  
  55.             for (int counter = 1; counter <= difference; counter++)
  56.             {
  57.                 factorialDifference *= counter;
  58.  
  59.             }
  60.             Console.WriteLine("The factorial of {0} is {1}", difference, factorialDifference);
  61.  
  62.             Console.WriteLine("N!*K! / (K-N)! = {0}", (factorialN * factorialK) /factorialDifference );
  63.         }
  64.     }
  65. }
Advertisement
Add Comment
Please, Sign In to add comment