Skip to content

Latest commit

 

History

8 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Typing SVG

Python Status License Made with

🎩✨ The Factorial Calculator ✨🎩

A tiny, elegant Python script that turns a single number into its full factorial — instantly.

magic animation


📖 Table of Contents


🧮 What is a Factorial?

The factorial of a non-negative integer n, written n!, is the product of every positive integer from 1 up to n.

n! = n × (n−1) × (n−2) × … × 2 × 1

n n!
0 1
1 1
2 2
3 6
4 24
5 120
10 3,628,800

Factorials show up everywhere — combinatorics, probability, permutations, series expansions — which makes this humble little script surprisingly powerful.


⚙️ How the Code Works

The script does three things, in order:

  1. Reads an integer from the user via input().
  2. Branches on the value of n:
    • 🔴 Negative → prints an error message (factorials aren't defined here).
    • 🟡 Zero → prints 1 directly (0! = 1 by mathematical convention).
    • 🟢 Positive → multiplies its way up from 1 to n in a for loop.
  3. Prints the final result.
n = int(input("Enter a number: "))
if n < 0 :
    print("Factorial is not defined for negative numbers.")
elif n == 0 :
    print("Factorial is: 1")
else:
    factorial = 1
    for i in range(1, n + 1):
        factorial = factorial * i
    print("Factorial is:", factorial)

Clean, readable, and dependency-free — pure Python standard library, no imports needed.


🔀 Program Flow

flowchart TD
    A(["🚀 Start"]) --> B["Read integer n"]
    B --> C{"n < 0 ?"}
    C -- "Yes" --> D["❌ Print: not defined for negatives"]
    C -- "No" --> E{"n == 0 ?"}
    E -- "Yes" --> F["✅ Print: Factorial is 1"]
    E -- "No" --> G["🔁 Loop i = 1 → n<br/>factorial *= i"]
    G --> H["✅ Print: Factorial is factorial"]
    D --> I(["🏁 End"])
    F --> I
    H --> I

    style A fill:#2ECC71,stroke:#27AE60,color:#fff
    style I fill:#E74C3C,stroke:#C0392B,color:#fff
    style D fill:#FF6B6B,stroke:#C0392B,color:#fff
    style F fill:#F7C948,stroke:#D4A017,color:#1a1a2e
    style H fill:#4ECDC4,stroke:#1B9C92,color:#1a1a2e
    style G fill:#6C5CE7,stroke:#4834D4,color:#fff
Loading

💡 GitHub renders Mermaid diagrams natively — this flowchart will animate into view when the README loads.


💻 Full Source Code

Click to expand factorial.py 📂
n = int(input("Enter a number: "))
if n < 0 :
    print("Factorial is not defined for negative numbers.")
elif n == 0 :
    print("Factorial is: 1")
else:
    factorial = 1
    for i in range(1, n + 1):
        factorial = factorial * i
    print("Factorial is:", factorial)


🚀 Getting Started

# 1️⃣ Clone or download this repo
git clone https://github.com/your-username/factorial-calculator.git
cd factorial-calculator

# 2️⃣ Run the script
python3 factorial.py

# 3️⃣ Enter any integer when prompted
Enter a number: 7
Factorial is: 5040

🛡️ Edge Cases Handled

Input Behavior
n < 0 Prints a friendly error — factorial isn't defined for negatives
n == 0 Correctly returns 1 (mathematical convention: 0! = 1)
n > 0 Computes the product iteratively via a for loop
Non-integer input ⚠️ Will raise a ValueError — not currently caught

📜 License

Released under the MIT License — free to use, modify, and share.

⭐ If this helped you understand factorials, consider giving it a star! ⭐

Made with 🐍 Python and a genuine appreciation for exclamation marks (n!)

Author: Hadiqa Hanif

About

A simple Python Code to the Calculate Factorial of any number.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages