Factorial in Powershell (original) (raw)

  1. Home
  2. Programming Projects in Every Language
  3. Factorial
  4. Factorial in Powershell

Published on 20 May 2025 (Updated: 20 May 2025)

Factorial in Powershell

Welcome to the Factorial in Powershell page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

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 { <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>P</mi><mi>r</mi><mi>o</mi><mi>d</mi><mi>u</mi><mi>c</mi><mi>t</mi><mo>∗</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">Product *= </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">ro</span><span class="mord mathnormal">d</span><span class="mord mathnormal">u</span><span class="mord mathnormal">c</span><span class="mord mathnormal">t</span><span class="mord">∗</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>_ }
    }

    $Product
}

if ($args.Length -lt 1) {
    Show-Usage
}

try {
    <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>V</mi><mi>a</mi><mi>l</mi><mi>u</mi><mi>e</mi><mo>=</mo><mo stretchy="false">[</mo><mi>i</mi><mi>n</mi><mi>t</mi><mo stretchy="false">]</mo><mo>:</mo><mo>:</mo><mi>P</mi><mi>a</mi><mi>r</mi><mi>s</mi><mi>e</mi><mo stretchy="false">(</mo></mrow><annotation encoding="application/x-tex">Value = [int]::Parse(</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">Va</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">u</span><span class="mord mathnormal">e</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mopen">[</span><span class="mord mathnormal">in</span><span class="mord mathnormal">t</span><span class="mclose">]</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">::</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:1em;vertical-align:-0.25em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal">rse</span><span class="mopen">(</span></span></span></span>args[0])
} catch {
    Show-Usage
}

if ($Value -lt 0) {
    Show-Usage
}

Write-Host (Get-Factorial $Value)

Factorial in Powershell was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.