Python Program for floor() Function

In the previous article, we have discussed Python Program for fabs() Function
floor() Function in Python:

If necessary, the math.floor() method rounds a number DOWN to the nearest integer and returns the result.

The python math. floor() function returns the nearest integer value that is less than or equal to the specified expression or Value.

Syntax:

math.floor(number)

Parameter Values:

number: This is required. It Sets the number to round down.

Return Value: The return value is an int value that represents the rounded number.

Examples:

Example1:

Input:

Given tuple = (1.3, 10.04, -4.4, 7.7, 9.45)
Given List = [2.8, 3.4, -1.7, -6.82, 12.2]

Output:

Given positive number 9.2 floor value = 9.00
Given negative number -8.6 floor value = -9.00
Given Tuple element(gvn_tupl[3]) floor value = 7.00
Given Tuple element(gvn_lst[2]) floor value = -2.00
The floor of pi is:  3
Given multiple number(35+40-10.3) floor value  = 64.00

Example2:

Input:

Given tuple = (2.4, 85.67, 37.8, -25.5)
Given List =  [45.9, 11.32, -25, 6, 12.8, -10]

Output:

Given positive number 30.1 floor value = 30.00
Given negative number -20.4 floor value = -21.00
Given Tuple element(gvn_tupl[0]) floor value = 2.00
Given Tuple element(gvn_lst[3]) floor value = 6.00
The floor of pi is:  3
Given multiple number(23-10+5) floor value  = 18.00

Program for floor() Function in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Import math module using the import keyword.
  • Give the tuple as static input and store it in a variable.
  • Give the list as static input and store it in another variable.
  • Apply math. floor() function to any positive number and print it.
  • Apply math. floor() function to any negative number and print it.
  • Apply math. floor() function to the given tuple element and print it.
  • Apply math. floor() function to the pi value and print it.
  • Apply math. floor() function to any multiple numbers and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as static input and store it in a variable.
gvn_tupl = (1.3, 10.04, -4.4, 7.7, 9.45)
# Give the list as static input and store it in another variable.
gvn_lst = [2.8, 3.4, -1.7, -6.82, 12.2]
# Apply math. floor() function to any positive number and print it.
print('Given positive number 9.2 floor value = %.2f' % math.floor(9.2))
# Apply math. floor() function to any negative number and print it.
print('Given negative number -8.6 floor value = %.2f' % math.floor(-8.6))
# Apply math. floor() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[3]) floor value = %.2f' % math.floor(
    gvn_tupl[3]))
# Apply math. floor() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[2]) floor value = %.2f' % math.floor(gvn_lst[2]))
# Apply math. floor() function to the pi value and print it.
print("The floor of pi is: ", math.floor(math.pi))
# Apply math. floor() function to any multiple numbers and print it.
print('Given multiple number(35+40-10.3) floor value  = %.2f' %
      math.floor(35+40-10.3))

Output:

Given positive number 9.2 floor value = 9.00
Given negative number -8.6 floor value = -9.00
Given Tuple element(gvn_tupl[3]) floor value = 7.00
Given Tuple element(gvn_lst[2]) floor value = -2.00
The floor of pi is:  3
Given multiple number(35+40-10.3) floor value  = 64.00

Method #2: Using Built-in Functions (User Input)

Approach:

  • Import math module using the import keyword.
  • Give the tuple as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in another variable.
  • Give the positive number as user input using the float(input()) function
  • Store it in another variable.
  • Apply math. floor() function to the given positive number and print it.
  • Give the negative number as user input using the float(input()) function and store it in another variable.
  • Apply math. floor() function to the given negative number and print it.
  • Apply math. floor() function to the given tuple element and print it.
  • Apply math. floor() function to the given list element and print it.
  • The Exit of Program.

Below is the implementation:

# Import math module using the import keyword.
import math
# Give the tuple as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_tupl = tuple(map(float, input(
   'Enter some random tuple Elements separated by spaces = ').split()))
# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in another variable.
gvn_lst = list(map(float, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Give the positive number as user input using the float(input()) function and
# Store it in another variable.
gvn_positvenum=float(input("Enter some random number = "))
# Apply math. floor() function to the given positive number and print it.
print('Given positive number',gvn_positvenum,'floor value = %.2f' % math.floor(gvn_positvenum))
# # Give the negative number as user input using the float(input()) function and
# Store it in another variable.
gvn_negatvenum=float(input("Enter some random number = "))
# Apply math. floor() function to the given negative number and print it.
print('Given negative number',gvn_negatvenum,' floor value = %.2f' % math.floor(gvn_negatvenum))
# Apply math. floor() function to the given tuple element and print it.
print('Given Tuple element(gvn_tupl[0]) floor value = %.2f' % math.floor(
    gvn_tupl[0]))
# Apply math. floor() function to the given list element and print it.
print(
    'Given Tuple element(gvn_lst[3]) floor value = %.2f' % math.floor(gvn_lst[3]))

Output:

Enter some random tuple Elements separated by spaces = 26.4 10.8 -11.23 -10.7
Enter some random List Elements separated by spaces = 89 45.2 65.3 96.8
Enter some random number = 54.3
Given positive number 54.3 floor value = 54.00
Enter some random number = -10.5
Given negative number -10.5 floor value = -11.00
Given Tuple element(gvn_tupl[0]) floor value = 26.00
Given Tuple element(gvn_lst[3]) floor value = 96.00

Read all the mathematical functions available in Python and understand how to implement them in your program by using the tutorial of Python Mathematical Methods Examples.