Skip to main content

S.Y.I.T Sem 3 Python Practical | Python| Python practical 1f|Write a recursive function to print the factorial for a given number. | python for beginner

 Hello reader's 

In this blog post we are going to see that how we can print the factorial for a given number using python.




To do this, first you have to know the basics like - What is recursive function? , What is factorial?, How to find the factorial of any given number. 

Now let's see it one by one, First of all we will understand that What is recursive Function?

Recursive function is a function that call itself after a fix interval of time. Using the recursive algorithm, certain problems can be solved quite easily. Towers of Hanoi (TOH) is one such programming exercise. Try to write an iterative algorithm for TOH. Moreover, every recursive program can be written using iterative methods.

Mathematically, recursion helps to solve a few puzzles easily.

For example, a routine interview question,

In a party of N people, each person will shake her/his hand with each other person only once. In total how many hand-shakes would happen? 

It can be solved in different ways; graphs, recursions, etc. Let us see how recursively it can be solved.

There are N persons. Each person shakes hands with each other only once. Considering N-th person, (s)he has to shake a hand with (N-1) the person. Now the problem is reduced to small instances of (N-1) persons. Assuming TN as a total shake-hands, it can be formulated recursively.

TN = (N-1) + TN-1 [T1 = 0, i.e. the last person has already shook-hand with every one]

Solving it recursively yields an arithmetic series, which can be evaluated into N(N-1)/2.

now we will see What is factorial?

The factorial of a number is the function that multiplies the number by every natural number below it. 

For example, 4 factorial, that is, 4! can be written as: 4! = 4×3×2×1 = 24.

How to find the factorial of any given number ?

The formula for n factorial is: 

                n!=n*(n-1)!

This means that the factorial of any number is, the given number, multiplied by the factorial of the previous number. So, 

8!= 8*7*6*5*4*3*2*1

9!= 9*8*7*6*5*4*3*2*1

and so on ......






Code - 

def recur_factorial(n):

    """function to return the factorial of a number using recursion"""

    if n == 1:

        return n

    else:

        return n*recur_factorial(n-1)


num = int(input("Enter a number: "))


if num<0:

    print ("Sorry , factorial does not exists for a negative number")

elif num ==0:

    print("The factorial of 0 is 1")

else:

    print("The factorial of",num,"is",recur_factorial(num))

Comments

Popular posts from this blog

ChatGPT| Advantages| Disadvantages| How to use ChatGPT

  What is Chat GPT? ChatGPT is a cutting-edge language model developed by OpenAI that has been trained on a massive corpus of text data from the internet. It uses a neural network architecture that allows it to understand and generate human-like responses to a wide range of prompts, from simple questions to complex conversations. At its core, ChatGPT is designed to replicate the experience of talking to a human being, while also leveraging the power of artificial intelligence to provide fast, accurate, and personalized responses. Unlike traditional chatbots that rely on pre-programmed responses, ChatGPT can generate responses on the fly based on the context of the conversation and the user's input. One of the key advantages of ChatGPT is its versatility. It can be used in a wide range of applications, from customer service and tech support to education and entertainment. For example, a company might use ChatGPT to provide customers with instant answers to their questions, while a t...

S.Y.I.T Sem 3 Python Practical | Python| Python practical 1a| Print name and age +100 | python for beginner

 Hello reader's , This blog is for those students who are studying in second year of information technology. This blog is for practical of python  subject which is in sem 3. In this blog you are going to see that how you can ask user to enter their name and age. Print out a message addressed to them that tells them the year that they will turn 100 year old. For performing this practical you must have install python on your laptop or pc. to download python  - https://www.python.org/downloads/  python website  go to the link and download python and you need a IDE which supports python,  i am using VS Code, you can use python IDE too.  vs code screen After the successful installation open ide and type the code or simply copy the code from given link in below and run it to see the output. code -  name = input("Enter your name: ") age = int(input("Enter your age: ")) year = str((2021 - age)+100) print(name + "will be 100 years old in the year"+year)

Print calendar using python | Python| Calendar using Python |python for beginner

Hello readers, Today, we are going to see that how can we print the calendar using python.   Print calendar using python  In this blog you are going to see that how can you print calendar using python programming language. To perform this activity you must know the basics of python because we are going to use python programming language.   To perform this activity you must have to install python on your laptop or desktop. If you don't know that how to install python then follow these steps.    To install python perform the following steps :- 1) Go to - https://www.python.org/downloads/  2) click "Download" . 3) Download the any latest python version from the given website . 4) When download complete just open your file explorer and double click on downloaded python file and install the python in which directory you want.  5) After the successful installation just double click on python icon on desktop to open python IDE.  python logo  Af...