Python Program to Generate a Multiplication Table

Python Program to Generate a Multiplication Table
Reading Time: 7 minutes

Introduction :

This Python program is designed to generate a multiplication table based on the input provided by the user. A multiplication table is a grid that displays the products of two integers from 1 to a specified range, typically 10. The program allows users to enter any number, and it will display the multiplication table for that number, showing its multiples up to 10.

Algorithm for the "Multiplication Table Generator" program:

  1. Start the program.
  2. Define the generate_multiplication_table function that takes one parameter number.
  3. Print a message to indicate that the multiplication table for the given number will be displayed.
  4. Start a for loop to iterate through the numbers 1 to 10 (inclusive) using the range(1, 11) Let the current iteration number be represented by the variable i.
  5. Within the loop, calculate the product of number and i and print a formatted string showing the multiplication operation and the result.
  6. End the loop.
  7. Start the main part of the program using the __name__ == “__main__”
  8. Try to execute the following code block within the try block:
  9. Prompt the user to input a number using the input function.
  10. Convert the user input to an integer using the int function and store it in the user_input variable.
  11. Call the generate_multiplication_table function, passing user_input as an argument.
  12. If the user enters a non-integer value, catch the ValueError exception within the except
  13. Print an error message to inform the user that they should enter a valid number.
  14. End the program.

The program aims to generate a multiplication table for a user-inputted number and display the results for the numbers 1 to 10. If the user enters a non-numeric value, the program will handle the exception gracefully and prompt the user to input a valid number.

Python Code for the "Multiplication Table Generator" program:

    
     def generate_multiplication_table(number):
    print(f"Multiplication Table for {number}:")
    for i in range(1, 11):
        print(f"{number} x {i} = {number * i}")
if __name__ == "__main__":
    try:
        user_input = int(input("Enter a number to generate its multiplication table: "))
        generate_multiplication_table(user_input)
    except ValueError:
        print("Invalid input! Please enter a valid number.")

    
   

Input :

    
     Enter a number to generate its multiplication table: 9
    
   

Output :

    
     Multiplication Table for 9:
9 x 1 = 9
9 x 2 = 18
9 x 3 = 27
9 x 4 = 36
9 x 5 = 45
9 x 6 = 54
9 x 7 = 63
9 x 8 = 72
9 x 9 = 81
9 x 10 = 90
    
   

Explanation :

  1. The generate_multiplication_table function takes a single parameter number, representing the input for which the multiplication table needs to be generated.
  2. Inside the function, it starts by printing a header line displaying the number for which the table is being generated.
  3. Then, it uses a for loop to iterate through the numbers 1 to 10 (inclusive) using the range(1, 11)
  4. During each iteration, the program calculates the product of the input number and the current iteration number i, and it prints a formatted string displaying the multiplication operation and its result.
  5. The main part of the program starts with a __name__ == “__main__” block, which ensures that the code inside it is only executed when the program is run directly, not when it’s imported as a module in another program.
  6. Inside the main block, the program tries to read user input using input(), and it converts the input into an integer using int().
  7. If the input provided by the user is a valid integer, it calls the generate_multiplication_table function, passing the user input as an argument.
  8. If the input is not a valid integer (e.g., if the user enters non-numeric characters), it catches the ValueError exception and displays an error message, prompting the user to enter a valid number.

Overall, this program provides a simple and interactive way for users to generate multiplication tables for any number they choose, making it useful for educational purposes or quick reference.