Saturday, 7 December 2024

Day 27 : Python Program to Print Table of a Given Number


 

def table(number, limit=10):

    print(f"Multiplication table of {number}:")

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

        print(f"{number} x {i} = {number * i}")

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

table(number)

Code Explanation:

1. Function Definition

def table(number, limit=10):

    print(f"Multiplication table of {number}:")

Parameters:

number: The number for which the multiplication table is generated.

limit: The maximum value up to which the multiplication table is calculated (default is 10).

Header Message:

The print statement outputs a message indicating the multiplication table being generated.

2. Loop to Generate Multiplication Table

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

    print(f"{number} x {i} = {number * i}")

How It Works:

range(1, limit + 1) generates integers from 1 to limit (inclusive).

The loop iterates through each number i in this range.

In each iteration:

The current value of i is multiplied by number.

The result (number * i) is formatted and printed in the form:

number x i = result

3. User Input and Function Call

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

table(number)

Input:

The user is prompted to enter an integer (number).

int(input(...)) converts the input string into an integer.

Function Call:

The table function is called with number as the argument. Since the limit parameter has a default value of 10, it does not need to be explicitly specified unless a custom limit is required.

#source code --> clcoding.com

0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (28) AI (33) Android (24) AngularJS (1) Assembly Language (2) aws (17) Azure (7) BI (10) book (4) Books (173) C (77) C# (12) C++ (82) Course (67) Coursera (223) Cybersecurity (24) data management (11) Data Science (127) Data Strucures (8) Deep Learning (20) Django (14) Downloads (3) edx (2) Engineering (14) Excel (13) Factorial (1) Finance (6) flask (3) flutter (1) FPL (17) Google (34) Hadoop (3) HTML&CSS (47) IBM (25) IoT (1) IS (25) Java (93) Leet Code (4) Machine Learning (59) Meta (22) MICHIGAN (5) microsoft (4) Nvidia (1) Pandas (4) PHP (20) Projects (29) Python (923) Python Coding Challenge (318) Python Quiz (4) Questions (2) R (70) React (6) Scripting (1) security (3) Selenium Webdriver (2) Software (17) SQL (42) UX Research (1) web application (8)

Followers

Person climbing a staircase. Learn Data Science from Scratch: online program with 21 courses