This week's code snippet, Factorial in Nim, is brought to you by Subete and the Sample Programs repo.
#[
Factorial in Nim
This program calculates the factorial of a non-negative integer.
]#
import os
import strutils
const UsageMessage = "Usage: please input a non-negative integer"
proc factorial(n: int): int =
# Calculates the factorial of n (n!).
if n == 0:
return 1
var result = 1
for i in 1..n:
result *= i
return result
# The main execution block:
block:
# Check 1: No input argument provided.
if paramCount() == 0:
quit(UsageMessage, 1)
let inputStr = paramStr(1)
# Check 2: Invalid Input (Empty String or Not a Number)
var n: int
try:
n = parseInt(inputStr)
except:
quit(UsageMessage, 1)
# Check 3: Invalid Input (Negative Number)
if n < 0:
quit(UsageMessage, 1)
# Print the calculated result.
echo factorial(n)Below you'll find an up-to-date list of articles by me on The Renegade Coder. For ease of browsing, emojis let you know the article category (i.e., blog: ✒️, code: 💻, meta: 💭, teach: 🍎)
- ✒️ The Cult of Efficiency Is a Plague
- ✒️ Missing the Forest for the Trees: Why You Struggle to Solve Problems
- ✒️ What It Feels Like to Be a Toddler Again: Learning a Language
- ✒️ Things I Don’t Want AI To Help Me With
- ✒️ Why I Rebel Against the Use of Generative AI
- ✒️ Buying a House Sucks
- ✒️ Smug Yet Unserious
- ✒️ 32 College Stories That Always Make Friends Laugh
- 💻 Why Does == Sometimes Work on Integer Objects in Java?
- 🍎 Online Exams Might Be Cooked
Also, here are some fun links you can use to support my work.
This document was automatically rendered on 2026-03-27 using SnakeMD.





