Write a program that prints a multiplication table for a given number.
- User
Write a program that prints a multiplication table for a given number.
Assistant
Write a program that prints a multiplication table for a given number.
The user wants a program that prints a multiplication table for a specific number. I need to decide which programming language to use. Python is a good default choice because it's readable and widely used. I should also probably provide a C++ or Java example as well, or just stick to Python but make it clear. Let's provide Python as the primary solution and maybe C++ as an alternative for lower-level context, but usually, a single, clear Python example is best unless specified otherwise.
Plan:
Python Implementation:
num = int(input(...))for i in range(1, 11):product = num * iprint(f"{num} x {i} = {product}")Refinement: Should I add error handling? E.g., if the user types "abc"? For a simple request like this, keeping it simple is usually better. I will provide a basic version and a slightly more robust version, or just one clean version.
Code Structure:
def print_multiplication_table(number):
print(f"Multiplication Table for {number}:")
for i in range(1, 11):
print(f"{number} x {i} = {number * i}")
# Main execution
try:
user_input = int(input("Enter a number: "))
print_multiplication_table(user_input)
except ValueError:
print("Please enter a valid integer.")
Let's also consider the user might want a specific range. The standard school table goes up to 10 or 12. Let's stick to 10 for simplicity but mention how to change it.
Drafting the response:
Let's stick to Python as the main answer but make the code clean and commented.
Python Code:
# Get the number from the user
num = int(input("Enter a number: "))
# Iterate from 1 to 10
print(f"Multiplication Table of {num}:")
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
C++ Code:
#include <iostream>
using namespace std;
int main() {
int num;
cout << "Enter a number: ";
cin >> num;
cout << "Multiplication Table of " << num << ":" << endl;
for (int i = 1; i <= 10; ++i) {
cout << num << " x " << i << " = " << (num * i) << endl;
}
return 0;
}
I will provide the Python version as the primary answer because it's the most accessible, and then include the C++ version as an alternative.
Final Polish:
Let's go with that structure. It covers the "how" and provides immediate utility.
Steps:
num.Looks good. I will present the Python solution first. Here are examples in Python and C++, as these are the most common languages used for this type of task.
Python is concise and easy to read. This script uses a for loop to iterate through numbers 1 to 10.
# Ask the user for input
try:
num = int(input("Enter a number to print its multiplication table: "))
print(f"Multiplication Table for {num}:")
print("-" * 20)
# Loop from 1 to 10
for i in range(1, 11):
# Calculate the product
product = num * i
# Print the result using an f-string
print(f"{num} x {i} = {product}")
except ValueError:
print("Please enter a valid integer.")
This is a standard implementation in C++ using the iostream library for input and output.
#include <iostream>
using namespace std;
int main() {
int num;
// Ask the user for input
cout << "Enter a number to print its multiplication table: ";
cin >> num;
cout << "Multiplication Table for " << num << ":" << endl;
cout << "--------------------" << endl;
// Loop from 1 to 10
for (int i = 1; i <= 10; ++i) {
cout << num << " x " << i << " = " << (num * i) << endl;
}
return 0;
}
If the user enters 5, the output will look like this:
Multiplication Table for 5:
--------------------
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