Sadanand Ghule Profile picture
Unlocking business potential through strategic insights. #BusinessAnalytics #DataAnalysis #DataScience #MachineLearning

Oct 15, 2022, 9 tweets

How to Find Factorial in Three Effective Ways

🧵Step by Step Solution

#Python #pythonprogramming #DataScience #DataAnalytics #Data #MachineLearning #Python

👇🏻

🚩Problem

Write a program which can compute the factorial of a given number.

Suppose the following input is supplied to the program:
4
Then, the output should be:
24

Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.

👇🏻

💡Concept of Factorial

- Factorial of 5 = 1 x 2 x 3 x 4 x 5 = 120
- Factorial of 0 = 1
- Factorial of 1 = 1
- Factorial of 2 = 1 x 2 = 2
- Factorial of -1 = Does not exist

👇🏻

✅Steps to Solve

1. Take the input and convert it to int data type
2. Check if the value is negative then the factorial does not exist
3. Check if the value is equal to zero, if true then return 1

👇🏻

4. use for loop to perform reverse iteration, to multiply values in decreasing order
5. return the result
6. call the function to test the output

💡Method 1: Reverse Iteration

👇🏻

💡Method 2: Recursive Function

👇🏻

Recursive Function: Visualisation of the program with the help of
pythontutor.com/visualize.html…

👇🏻

💡Method 3: While Loop

👇🏻

If you appreciate my explanation style, please follow me for more information like this.

Let us all grow together.

Like, Share, RT - first tweet

comment what you feel...

Share this Scrolly Tale with your friends.

A Scrolly Tale is a new way to read Twitter threads with a more visually immersive experience.
Discover more beautiful Scrolly Tales like this.

Keep scrolling