Back

Explore Courses Blog Tutorials Interview Questions
0 votes
4 views
in Python by (16.4k points)
closed by

How might I combine these two functions into one recursive function to have this outcome: 

factorial(6)

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

Look at the below code:

def factorial( n ):

   if n <1:   # base case

       return 1

   else:

       return n * factorial( n - 1 )  # recursive call

def fact(n):

       for i in range(1, n+1 ):

               print "%2d! = %d" % ( i, factorial( i ) )

fact(6)

1! = 1

2! = 2

3! = 6

4! = 24

5! = 120

6! = 720

as you see the execution of these two offers the right response, I simply need to make it to one recursive function.

closed

4 Answers

0 votes
by (19k points)
 
Best answer
To combine factorial calculation and printing factorial expressions into a single recursive function, you can modify the factorial() function to print expressions while calculating. Here's a concise example:

def factorial(n):

    if n < 1:

        return 1

    else:

        result = n * factorial(n - 1)

        print("%d! = %d" % (n, result))

        return result

factorial(6)

In this modified code, the factorial() function recursively calculates the factorial while printing expressions. When calling factorial(6), it will generate the desired output, including factorial expressions and their corresponding values.

Note: If you only want to print the final factorial value without expressions, remove the print statement from the factorial() function and use print(factorial(6)) instead.
0 votes
by (26.4k points)

Try the below code:

def factorial( n ):

   if n <1:   # base case

       return 1

   else:

       returnNumber = n * factorial( n - 1 )  # recursive call

       print(str(n) + '! = ' + str(returnNumber))

       return returnNumber

Interested to learn the concepts of Python in detail? Come and join the python course to gain more knowledge in Python

Watch this video tutorial on how to become a professional in python

0 votes
by (25.7k points)
To combine the two functions into a single recursive function, you can modify the factorial() function to print the factorial expressions as it calculates the factorial value. Here's an example of how you can achieve this:

def factorial(n):

    if n < 1:   # base case

        return 1

    else:

        result = n * factorial(n - 1)  # recursive call

        print("%d! = %d" % (n, result))  # print factorial expression

        return result

factorial(6)

In this updated code, the factorial() function now calculates the factorial recursively, and each time it makes a recursive call, it also prints the factorial expression. This way, when you call factorial(6), it will print the desired output with factorial expressions for each number from 1 to 6.

Note: If you only want to print the final result without the intermediate expressions, you can remove the print statement from the factorial() function and call it as print(factorial(6)) instead.
0 votes
by (15.4k points)
To combine the functionality of calculating factorials and printing factorial expressions into a single recursive function, you can modify the factorial() function to print the expressions while calculating the factorial value. Here's an example of how you can achieve this:

def factorial(n):

    if n < 1:   # base case

        return 1

    else:

        result = n * factorial(n - 1)  # recursive call

        print("%d! = %d" % (n, result))  # print factorial expression

        return result

factorial(6)

In this revised code, the factorial() function recursively calculates the factorial value for each number from 1 to the given input n. As it performs the recursive calls, it also prints the factorial expression, such as "3! = 6", for each number. Thus, when you call factorial(6), it will generate the desired output, displaying both the factorial expressions and their corresponding values.

Note: If you only wish to print the final factorial value without the intermediate expressions, you can remove the print statement from the factorial() function and call it as print(factorial(6)) instead.

Browse Categories

...