HomePythonPython Program to Multiply Two Floating Point Numbers

Python Program to Multiply Two Floating Point Numbers

In this python tutorial, you will learn how to Multiply two floating point numbers using the arithmetic operator along with other functions like the input function, format function, float function and print function of the python programming language.

How to Multiply Two Floating Point Numbers in Python?

Let’s take a look at the first source code , here the values are assigned in the code and the multiplication operator carries out the function.

RUN CODE SNIPPET
#Program to multiply two float numbers
num1 = 8.2
num2 = 2.4

#Multiplying two float numbers
product = float(num1)*float(num2)

#Displaying the output value
print("The product of {0} and {1} is {2}".format(num1, num2, product))

OUTPUT:

The product of 8.2 and 2.4 is 19.679999999999996

  1. At the start, we are declaring two variables num1 and num2 and assigning the values as 8.2 and 2.4 respectively.
  2. We multiply two numbers num1 and num2 using the arithmetic operation *,  we declare the values with the  float function which converts a number stored in a string or integer into a floating point number, that is a number with a decimal point. We store the value in the variable product.
  3. Now, we display the output value using the print and display the statement/string "The product of {0} and {1} is {2}". format(num1, num2, product).
  4. In the above statement/string, the variables {0} , {1} and {2} will hold the values num1, num2  and product where the format function helps in variable substitution and data formatting.

Let’s take a look at the second source code , here the values are given as input by the user in the code and the multiplication operator carries out the function.

RUN CODE SNIPPET
#Program to multiply two float numbers
num1 = input('\n Enter the first number: ')
num2 = input('\n Enter the second number: ')

#Multiplying two float numbers
product = float(num1)*float(num2)

#Displaying the output value
print("\n The product of {0} and {1} is {2}".format(num1, num2, product))

INPUT:

8.2
2.4

OUTPUT:

Enter the first number:
Enter the second number:
The product of 8.2 and 2.4 is 19.679999999999996

  1. Here we give the user the option to enter the values and the input values are scanned using the input function and are stored in two variables namely num1 and num2  with the statements/strings Enter First number: and Enter Second number: with  \n respectively.
  2. In the STDIN section of the code editor the input values are entered.
  3. When multiplying the  two numbers num1 and num2 using the arithmetic operation * , we declare the input values with the  float function and store the value in the variable product.
  4. As given in the first program, the variables {0} , {1} and {2} will hold the values num1, num2  and product where the format function helps in variable substitution and data formatting.


NOTE:

  1. The Multiplication operator (*) is used to multiply integers in a program and the final value is stored in a variable named as product.
  2. The variables used by the format function for substitution is enclosed in curly braces.
  3. The print statement is followed by a period, to initiate the format function.
  4. The print statement/string to be displayed in enclosed in double quotes.
  5. The statement for the input function are enclosed in single quotes and parenthesis.
  6. The \n in the code indicates a new line or the end of a statement line or a string.

Leave a Reply

You May Also Like

In this Python program, we will create a singly linked list and remove duplicate elements from it. A linked list...
This Python program solves the Celebrity Problem by finding a person who is known by everyone but does not know...
This Python program uses a recursive approach to solve the n-Queens problem. It explores all possible combinations of queen placements...