Skip to main content
  • Python is available now. All other compilers are coming soon.

    Python
  • Popular Tutorials

    • Getting Started With Python
    • Python if Statement
    • while Loop in Python
    • Python Lists
    • Dictionaries in Python
    All Python Tutorials

    Reference Materials

    Built-in FunctionsList MethodsDictionary MethodsString Methods
    View all

Online Compilers

  • Python Compiler
  • Python Playground
  • Python Projects
  • Exercises

Tutorials

  • Learn Python
  • Python for Beginners
  • Variables and Types
  • Python Strings
  • Python Lists
  • Python If Else
  • Python For Loops
  • Python Functions
  • Python Classes

Examples

  • Python Examples
  • Hello World
  • Add Two Numbers
  • Even or Odd
  • Factorial
  • Fibonacci
  • Prime Check
  • Palindrome Check

Company

  • About
  • Contact
  • Privacy Policy
  • Terms of Use
  • Disclaimer

© 2026 ILoveCompiler. All rights reserved.

  1. Home
  2. Python Exercises
  3. Factorial

Loops and patterns · Python exercise

Factorial

Compute n! with a loop, including the special case 0! = 1.

beginner4 testsloopsfactorialaccumulator

Problem

Read a non-negative integer n and print n! (the product of every integer from 1 to n). By definition, 0! is 1.

Input and output

Input
One line containing a non-negative integer n.
Output
A single line containing n!.

Constraints

  • 0 ≤ n ≤ 20.

Examples

Example 1

Input

5

Output

120

Example 2

Input

0

Output

1

The empty product is 1.

How testing works

Your code runs once per test case with that case's standard input. Output is compared after normalising trailing spaces and blank lines at the end. Use input() without a prompt when the exercise expects clean output.

Your solution4 hidden input checks

Loading editor…

Press Run tests to execute your code against every input case. Python runs in a Web Worker in your browser.

Related exercises

beginner

First n Fibonacci Numbers

Print the first n terms of the Fibonacci sequence on one line.

3 tests
intermediate

Greatest Common Divisor

Compute the greatest common divisor of two positive integers.

4 tests
Previous exerciseGreatest Common DivisorNext exerciseFirst n Fibonacci Numbers