Multiplication Facts Program in Python

Answered using GPT-4o mini by AI Answer Generator

Question:

Program 2: “Multiplication Facts” The intent of this program is to allow a user to enter an integer and then be presented with the first 10 multiples (multiplication facts) of that integer. The user must be notified of a sentinel value. The user will continue to be prompted to enter an integer and see the multiples until the sentinel value is entered at which point the program will stop. Use a while loop to get the inputs and a for loop to print out the facts:

  1. The first output should be a program header as described above like: ########## Program #2 – Multiplication Facts ###########
  2. a while loop with a user prompt to enter either an integer or sentinel value
  3. a for loop to output the formatted multiplication facts, one per line, as in this example: • 5 x 1 = 5 • 5 x 2 = 10 • 5 x 3 = 15, etc. • You will need to select a sentinel.
Answer:

Here's a Python program that implements the requirements you've outlined for the "Multiplication Facts" program. The program will prompt the user to enter an integer and display the first 10 multiples of that integer. The user can continue entering integers until they enter a sentinel value to exit the program.

# Program Header
print("########## Program #2 – Multiplication Facts ###########")

# Sentinel value
sentinel = -1  # You can choose any value that makes sense as a sentinel

while True:
    # Prompt user for input
    user_input = input(f"Enter an integer to see its multiplication facts (or {sentinel} to exit): ")

    # Convert input to integer
    try:
        number = int(user_input)
    except ValueError:
        print("Please enter a valid integer.")
        continue

    # Check for sentinel value
    if number == sentinel:
        print("Exiting the program.")
        break

    # Print multiplication facts using a for loop
    print(f"Multiplication facts for {number}:")
    for i in range(1, 11):
        result = number * i
        print(f"{number} x {i} = {result}")

Explanation of the Code:

  1. Program Header: The program starts by printing a header to indicate the purpose of the program.
  2. Sentinel Value: A sentinel value of -1 is defined. This value will be used to exit the loop.
  3. While Loop: The program enters a while True loop, which will continue until the user enters the sentinel value.
  4. User Input: The program prompts the user to enter an integer or the sentinel value. It uses a try-except block to handle invalid inputs (non-integer values).
  5. Sentinel Check: If the user enters the sentinel value, the program prints an exit message and breaks out of the loop.
  6. For Loop: If a valid integer is entered, a for loop runs from 1 to 10, calculating and printing the multiplication facts for the entered integer.

Usage:

  • Run the program, and it will prompt you to enter an integer.
  • Enter an integer to see its multiplication facts.
  • Enter -1 (or the chosen sentinel value) to exit