Python Program To Multiply Two Numbers

Problem Definition

Create a Python program to multiply two numbers.

Program

num_1 = 2
num_2 = 3 
product = num_1 * num_2 
print("Product of {} and {} is {}".format(num_1, num_2,product))

Output

Product of 2 and 3 is 6

First, the two numbers are stored in the variables num_1 and num_2, respectively. Multiplication in Python is done by ( * ) operator, Then the result is saved in the product variable and printed out using string formatting.

To learn more about string formatting in Python read –¬†How To Use String Formatting In Python

However, the more memory-efficient way to perform a multiplication in Python is by not using any variables at all it can be done in a line, but it can make the code hard to read.

print(2*3)

Output

6

Problem Definition

Create a Python Program to multiply two numbers provided by the user in real-time.

Program

num_1 = input("Enter the first number")
num_2 = input("Enter the second number")

product = float(num_1) * float(num_2) 

print("Product of {} and {} is {}".format(num_1, num_2,product))

Output

Enter the first number 2
Enter the second number 3
Product of 2 and 3 is 6.0

In this program first, we are taking user input with the built-in input() function of Python then before multiplying the numbers we are converting the inputs to float type using the float() function because the input() function returns the object as a string.

To learn more about Python data types read the following articles.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Close