🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
The multiplication table of a number is a foundational concept in arithmetic and a common programming task that can be used to teach loops and output formatting in Python. Creating a table for a number involves multiplying that number by a series of consecutive integers.
A multiplication table for a number displays that number multiplied by a range of other numbers, usually from 1 to 10. Each line of the table shows the result of the number multiplied by each of these integers in succession.
2. Program Steps
1. Choose a number to generate the table for.
2. Decide the range of multiples (commonly 1 to 10).
3. Loop through the range, multiplying the chosen number by each integer in the range.
4. Print the results in a formatted manner.
3. Code Program
# Function to print the multiplication table of a number
def print_table(number):
# Define the range of multiples
for i in range(1, 11):
# Calculate the product
product = number * i
# Print the formatted multiplication line
print(f"{number} x {i} = {product}")
# Choose a number
num = 5
# Call the function to print the table
print_table(num)
Output:
5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 5 x 4 = 20 5 x 5 = 25 5 x 6 = 30 5 x 7 = 35 5 x 8 = 40 5 x 9 = 45 5 x 10 = 50
Explanation:
1. A function called print_table is defined with a parameter number, which is the number for which the table will be generated.
2. A for loop iterates through the range 1 to 10, representing the multiples.
3. In each iteration, the product of the number and the loop counter i is calculated.
4. The multiplication fact is printed out in a formatted string using Python's f-string notation f"{number} x {i} = {product}".
5. After defining the function, it is called with num as the argument, which is set to 5.
6. The function prints out the multiplication table for 5, from 5 x 1 up to 5 x 10.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment