Venciity

[ Telerik C#] LOOPS - EX 4

Feb 9th, 2014
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.43 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.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.             Console.WriteLine("N! / K! = {0}", factorialN/factorialK);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment