Print the Factorial of a Given Number
Calculate and print the factorial of a number.
BeginnerPhase 2 - Level 1: Basic LoopingExample 4 of 14
print-the-factorial-of-a-given-number.py
1# Take n as input2n = int(input("Enter n: "))34# Calculate factorial5factorial = 16for i in range(1, n + 1):7 factorial *= i89print(f"Factorial of {n} is {factorial}")
Output
Enter n: 5 Factorial of 5 is 120
What's going on
Multiply numbers from 1 to n.
Key Concepts:
Initialize factorial to 1
Multiply by each number from 1 to n
5! = 1 * 2 * 3 * 4 * 5 = 120
🔗Related Content
- 📖
Phase 2 - Learn Concepts
Review Phase 2 concepts and explanations
- 📝
Phase 2 - Quiz
Test your Phase 2 understanding with quiz questions
- 💻
Phase 2 - All Practice Problems
Explore all practice problems for Phase 2
- 🎓
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- 🧠
Logic Building Overview
Learn about the complete logic building curriculum