codescracker
python

Python Program to Print Multiplication Table using Recursion



« Previous Program Next Program »

This article deals with program in Python that find and print multiplication table of a number using recursive function. The number must be entered by user at run-time.

Print Table using Recursion

The question is, write a Python program to print table of a given number using recursion. The program given below is answer to this question:

def tab_rec_fun(n, i):
    if(i>10):
        return
    print(n, "*", i, "=", n*i)
    return tab_rec_fun(n, i+1)

print("Enter the Number: ")
num = int(input())
print("\nTable of", num, "is:")
tab_rec_fun(num, 1)

Here is the initial output produced by above Python program:

print multiplication table python

Now supply the input say 6 as number to find and print its table using recursive function in Python, like shown in the sample output given below:

print multiplication of number python

Modified Version of Previous Program

In this program, I've used try-except block to handle and provide the manual error message when user try to enter any invalid input. Rest of the things are almost similar to previous program.

def tableFun(n, i):
    if(i>10):
        return
    print(n, "*", i, "=", n*i)
    return tableFun(n, i+1)

print("Enter a Number: ", end="")
try:
    num = int(input())
    print("\n-------Table of", num, "--------")
    tableFun(num, 1)
except ValueError:
    print("\nInvalid Input!")

Here is its sample run with user input, 132:

python print multiplication table

And here is another sample run with user input c:

python print table using recursion

Python Online Test


« Previous Program Next Program »



© Copyright 2021. All Rights Reserved.

CodesCracker