Advertisement
GustavAT

factorial of a number

Jun 10th, 2016
10
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.64 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Examples
  4. {
  5.     internal static class FactorialProgram
  6.     {
  7.         public static int Factorial(this int number)
  8.         {
  9.             if (number < 0) throw new ArgumentException("negative Zahl!");
  10.             return number > 1 ? number * Factorial(--number) : 1;
  11.         }
  12.  
  13.         public static void Main(string[] args)
  14.         {
  15.             Console.Write("Nicht-negative ganze Zahl: ");
  16.            
  17.             int value;
  18.             if (!int.TryParse(Console.ReadLine(), out value)) return;
  19.  
  20.             Console.Write("{0}! = {1}", value, value.Factorial());
  21.             Console.Read();
  22.         }
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement