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

Cafe management software| Restaurant management system| Inventory management for cafes| Cafe billing system

 Cafes have become a popular spot for people to hang out, have meetings or just grab a quick bite. With the increasing number of cafes in cities, managing them efficiently has become a crucial task. This is where a cafe management system comes into play. In this blog post, we will discuss what a cafe management system is, its features and benefits. A cafe management system is a software that helps cafe owners to manage various aspects of their cafe, such as billing, inventory, customer management, employee management and sales tracking. The system automates most of the mundane tasks, freeing up the owner's time to focus on more important aspects of running the cafe. Features of a Cafe Management System: Billing: The cafe management system should have a billing feature that allows the cafe owner to create bills quickly and accurately. It should also have the capability to generate invoices and receipts. Inventory Management: The system should be able to manage the cafe's invento...

DALL-E AI, AI-generated images, GPT-3, OpenAI, artificial intelligence, natural language processing, deep learning, neural networks, image synthesis, image recognition, visual content creation.

 DALL.E is an AI system that can create images from text descriptions, using a large neural network trained on a dataset of text-image pairs. It was introduced by OpenAI in January 2021, and has since been improved with DALL.E 2, which generates more realistic and accurate images with higher resolution. In this blog post, I will explain how DALL.E works and show some examples of its amazing capabilities. DALL.E is based on GPT-3, a transformer language model that can generate text for a wide range of tasks. DALL.E extends GPT-3 by adding image tokens to its vocabulary, allowing it to generate both text and images in a single stream of data. DALL.E receives both the text and the image as input, and is trained to generate all of the tokens, one after another, using maximum likelihood. To represent images, DALL.E uses a discrete variational autoencoder (VAE) that compresses each image to a 32x32 grid of discrete latent codes. Each code corresponds to one of 8192 possible image tokens....

Automation and robotics| automation, robotics, artificial intelligence, machine learning, industrial revolution, manufacturing, productivity, efficiency, human-robot collaboration, automation technology, future of work, job displacement, innovation

 Automation and robotics are two technologies that have revolutionized the manufacturing industry. They have enabled manufacturers to produce goods more efficiently and with higher quality than ever before. Automation refers to the use of technology to perform tasks without human intervention, while robotics involves the use of machines to perform tasks that would otherwise require human labor. This blog post will discuss the history, benefits, and challenges of automation and robotics in the manufacturing industry. History of Automation and Robotics The history of automation and robotics can be traced back to the early 20th century when Henry Ford introduced the assembly line. The assembly line allowed workers to specialize in specific tasks and increased productivity. The introduction of programmable logic controllers (PLCs) in the 1970s further advanced automation by allowing machines to be controlled by computers. The first robots were developed in the 1950s and were primarily ...