diff --git a/archive/p/powershell/Factorial.ps1 b/archive/p/powershell/Factorial.ps1 new file mode 100644 index 000000000..e9c12caf5 --- /dev/null +++ b/archive/p/powershell/Factorial.ps1 @@ -0,0 +1,29 @@ +function Show-Usage() { + Write-Host "Usage: please input a non-negative integer" + Exit 1 +} + +function Get-Factorial([int]$Value) { + $Product = 1 + if ($Value -gt 0) { + 1..$Value | ForEach-Object { $Product *= $_ } + } + + $Product +} + +if ($args.Length -lt 1) { + Show-Usage +} + +try { + $Value = [int]::Parse($args[0]) +} catch { + Show-Usage +} + +if ($Value -lt 0) { + Show-Usage +} + +Write-Host (Get-Factorial $Value)