(Python) Write a program that asks the user to input an integer. The program should output all the factors of the integer in a multiplication table format. The program should not output any repeats. The last line should be the square root of the number if it is a perfect square

Respuesta :

Answer:      

To write a program that outputs the factors of an integer in a multiplication table format, you can follow these steps:

1. Ask the user to input an integer.

2. Convert the user input into an integer type.

3. Create an empty list to store the factors.

4. Iterate over a range from 1 to the integer inputted by the user.

5. Check if the current number in the range is a factor of the inputted integer.

6. If it is a factor, add it to the list of factors.

7. After the loop, calculate the square root of the inputted integer.

8. Check if the square root is an integer (i.e., a perfect square).

9. If it is, add the square root to the list of factors.

10. Output the factors in a multiplication table format.

Here's an example implementation in Python:

```python

import math

# Step 1: Ask the user to input an integer

user_input = input("Please enter an integer: ")

# Step 2: Convert the user input into an integer type

number = int(user_input)

# Step 3: Create an empty list to store the factors

factors = []

# Step 4: Iterate over a range from 1 to the inputted number

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

# Step 5: Check if the current number is a factor

if number % i == 0:

# Step 6: Add the factor to the list

factors.append(i)

# Step 7: Calculate the square root of the inputted number

square_root = math.sqrt(number)

# Step 8: Check if the square root is an integer (perfect square)

if square_root.is_integer():

# Step 9: Add the square root to the list of factors

factors.append(int(square_root))

# Step 10: Output the factors in a multiplication table format

for factor in factors:

print(f"{factor} x {number // factor} = {number}")

# Example output for input 12:

# 1 x 12 = 12

# 2 x 6 = 12

# 3 x 4 = 12

```

This program takes an integer input from the user and outputsks the user to input an integer. The program should output all the factors of the integer in a multiplication table format. The program should not output any repeats. The last line should be the square root of the number if it is a perfect square

Explanation: