Skip to main content

Command Palette

Search for a command to run...

DAY 6 of PYTHON top 100 questions : from Basic to Advanced !!

Updated
2 min read
DAY 6 of PYTHON top 100 questions : from Basic to Advanced !!
P

Hello, I'm Priya Chakraborty, a dedicated B.Tech student in Electrical Engineering at Siliguri Institute Of Technology. I'm an enthusiastic learner, constantly seeking to expand my skill set and knowledge base. With a solid foundation in programming languages such as C, R, Python, and MySQL, I'm poised to tackle complex technical challenges.

But my passions extend beyond the realm of engineering. I'm also an aspiring content writer, driven by a curiosity to communicate ideas, both technical and non-technical, in a way that captivates and educates.

My journey is defined by a relentless pursuit of self-improvement, coupled with strong communication skills. I believe in the power of continuous learning and the importance of sharing knowledge.

Write a Python program to find the factorial of a given number:

n=int(input('Enter the number :  '))
fact=1
for i in range(1,n+1):
    fact=fact*i
print(fact)

Output :


Enter the number :  6
720

This is a Python program to calculate the factorial of a given number. The user is prompted to enter a number and the program uses a for loop to calculate the factorial. The factorial of a number is the product of all positive integers up to and including that number.

Let's go through the code line by line:

  1. n=int(input('Enter the number: ')) - This line prompts the user to enter a number and stores it in the variable n. The int function is used to convert the input to an integer.

  2. fact=1 - This line initializes the variable fact to 1. This variable will be used to store the factorial.

  3. for i in range(1,n+1): - This line sets up a for loop that will iterate over the range of numbers from 1 to n+1.

  4. fact=fact*i - This line multiplies the current value of fact by the loop variable i and stores the result back in fact.

  5. print(fact) - This line prints the final value of a fact, which is the factorial of the input number.

Overall, this program is a simple and effective way to calculate the factorial of a given number in Python.


If you are a beginner and want to know more about Python programming, you can read my Basics of Python blogs (From part 1 to part 15).


HAPPY LEARNING !!!

More from this blog

Priya's blog

46 posts

Python Developer@Codeclause Mentee @Trailhead Salesforce Content Executive @GDSC SIT'22-23 Google Women Techmakers Ambassador @Google WTM Coreteam @ GirlScript Kolkata