Advertisement
ivandrofly

Factorial - C#

Jun 3rd, 2014
420
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.70 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Factorial
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             int factorial;
  10.             while (!int.TryParse(Console.ReadLine(), out factorial))
  11.             {
  12.                 Console.WriteLine("Invalid number entered");
  13.             }
  14.             int result = 0;
  15.             bool hit = true;
  16.             for (int i = factorial - 1; i > 0; i--)
  17.             {
  18.                 if (hit)
  19.                 {
  20.                     result = factorial;
  21.                     hit = false;
  22.                 }
  23.                 result *= i;
  24.             }
  25.             Console.WriteLine(result);
  26.             Console.ReadLine();
  27.         }
  28.     }
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement