525,605 views
23 votes
23 votes
Write a program that takes a decimal number from the user and then prints the integer part and the decimal part separately. For example, if the user enters 2.718, the program prints: Integer part = 2 and decimal part = .718 in python

User Paolo B
by
2.7k points

1 Answer

15 votes
15 votes

Answer:

Step-by-step explanation:

The following was coded in Python as requested. It is a function that takes in a number as a parameter. It then uses the Python built-in math class as well as the modf() method to split the whole number and the decimal, these are saved in two variables called frac and whole. These variables are printed at the end of the program. The program has been tested and the output can be seen below.

import math

def seperateInt(number):

frac, whole = math.modf(number)

print("Whole number: " + str(math.floor(whole)))

print("Decimals number: " + str(frac))

Write a program that takes a decimal number from the user and then prints the integer-example-1
User Viswa
by
2.0k points