Factorial of a Number using Python.

 

Topic: Factorial of a Number using Python.

Hey Guys, today we are going see a Python Program to calculate the factorial of a given number.

Title: Python Code for Factorial of a Number with user input. (Beginner Friendly+ Easy + Code + Output + Online Compiler)


What is this program and why we are going to use it?

This Program consists of Variables, Input Function, Loop (while loop / for loop) and Arithmetic Operator.

By using variables, we are going to store values in variables in int format. By using input function, we are going to input values from the user. For example, storing values:
n = 5
Here n is a variable where 5 is assigned to n.

Now for input function

n = int(input("Enter a Number: "))
So, this will ask user to input a number.

Logic:

1. Take a number from user.
2. Initialize a variable fact = 1.
3. Use a loop from 1 to n (or while n > 0).
   - Multiply each number with fact
4. Display the factorial.

Code:


Online Compiler (Switch to Interactive Mode before execution):



Example Output:

Input: 5
Output: Factorial is: 120

Applications:

1. Used in mathematics (permutations and combinations)
2. Helps in understanding loops and logic building
3. Used in algorithms and problem-solving

Comments