24.0k views
1 vote
2.4 Code Practice: Question 2

Write a program that accepts a number as input, and prints just the decimal portion. Your program should also work if a negative number is inputted by the user.

Sample Run
Enter a number: 15.789
Sample Output
0.789
Hints:
You'll likely need to use both the int() and float() commands in your program.
Make sure that any number the user inputs is first stored as a float.
You'll need to use at least two variables in your program: one that stores the original value entered by the user, and one that represents the integer value entered by the user.
These two variables can be subtracted from one another, to leave just the decimal portion remaining.
Don't forget to have your program print the decimal portion at the end!

User Nona
by
6.9k points

1 Answer

4 votes

num = float(input("Enter a number: "))

num1 = int(num)

print(num - num1)

I hope this helps!

User Bkribbs
by
6.3k points