150k views
3 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 MoXplod
by
4.6k points

1 Answer

2 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 Kenan Banks
by
5.4k points