File tree Expand file tree Collapse file tree 1 file changed +93
-1
lines changed
Interview/Python/Solutions/140+ Basic Python Programs/Program 16 Expand file tree Collapse file tree 1 file changed +93
-1
lines changed Original file line number Diff line number Diff line change 1- # Program16
1+ # β¨ Factorial Calculator (Python)
2+
3+ This Python program calculates the ** factorial** of a given non-negative integer using a ` for ` loop.
4+
5+ ---
6+
7+ ## π Problem Statement
8+
9+ ** Write a Python program to find the factorial of a number.**
10+
11+ ---
12+
13+ ## βΉοΈ What is a Factorial?
14+
15+ The ** factorial** of a number ` n ` (written as ` n! ` ) is the product of all positive integers from ` 1 ` to ` n ` .
16+
17+ ### β
Examples:
18+
19+ - ` 5! = 5 Γ 4 Γ 3 Γ 2 Γ 1 = 120 `
20+ - ` 0! = 1 ` (by definition)
21+
22+ ---
23+
24+ ## β
Sample Code
25+
26+ ``` python
27+ # Python program to find the factorial of a number
28+
29+ # Input from user
30+ num = int (input (" Enter a non-negative integer: " ))
31+
32+ # Factorial calculation
33+ if num < 0 :
34+ print (" Factorial is not defined for negative numbers." )
35+ else :
36+ factorial = 1
37+ for i in range (1 , num + 1 ):
38+ factorial *= i
39+ print (f " The factorial of { num} is { factorial} " )
40+ ```
41+
42+ ---
43+
44+ ## βΆοΈ Example Runs
45+
46+ ``` bash
47+ Enter a non-negative integer: 5
48+ The factorial of 5 is 120
49+ ```
50+
51+ ``` bash
52+ Enter a non-negative integer: 0
53+ The factorial of 0 is 1
54+ ```
55+
56+ ``` bash
57+ Enter a non-negative integer: -3
58+ Factorial is not defined for negative numbers.
59+ ```
60+
61+ ---
62+
63+ ## π How to Run
64+
65+ 1 . Save the code in a file named ` factorial.py `
66+ 2 . Run the script using:
67+
68+ ``` bash
69+ python factorial.py
70+ ```
71+
72+ ---
73+
74+ ## π Suggested Project Structure
75+
76+ ```
77+ factorial/
78+ βββ factorial.py
79+ βββ README.md
80+ ```
81+
82+ ---
83+
84+ ## π‘ You Can Try
85+
86+ - Implement the factorial using ** recursion** .
87+ - Use the built-in ` math.factorial() ` function for comparison.
88+ - Plot the growth of factorial values using ` matplotlib ` .
89+
90+ ---
91+
92+ Happy Coding! π’π―
93+
You canβt perform that action at this time.
0 commit comments