Open In App

Python Program That Prints Out the Decimal Equivalents of 1/2, 1/3, 1/4, . . . ,1/10

Last Updated : 27 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will look into how to print out the decimal equivalents of 1/2, 1/3, 1/4,…,1/10 using Python.

Example:

Input number: 2
Output : 0.5
Explanation : Divide 1 by 2 to get the decimal equivalent of 1/2.

Example 1:

If we want to find equivalents of 1/2 we can simply use this code. In this code we simple store the result of 1/2 in any variable say ‘i’ and display the output.

Python3




# Declaring variable 'i' with value '1/2'
i = 1/2
# Printing i
print(i)


Output

0.5

Example 2:

Now, if we want to print out the decimal equivalents of 1/2, 1/3, 1/4,…,1/10 we will use for loop. In this code we have to run the for loop from range 2 to 10 and inside the for loop, we are printing the result of 1 divided by current number.

Python3




a = 1
# Running for loop from 2 to 10
for b in range(2, 11):
    print(f"{a}/{b} :", a/b)


Output

1/2 : 0.5
1/3 : 0.3333333333333333
1/4 : 0.25
1/5 : 0.2
1/6 : 0.16666666666666666
1/7 : 0.14285714285714285
1/8 : 0.125
1/9 : 0.1111111111111111
1/10 : 0.1

Example 3: 

If we want to print decimal equivalents on basis of inputs. In this code we will be following the same procedure as of example 2 but in this code instead of taking fixed value we asked the user to insert the range of their choice.

Python3




# Store the input from user in 'ab'
ab = int(input("Write the range: "))
a = 1
# Running the loop from 2 to input
# entered by the user.
for b in range(2, ab+1):
    print(f"{a}/{b} :", a/b)


Input:

Write the range: 20

Output:

1/2 : 0.5
1/3 : 0.3333333333333333
1/4 : 0.25
1/5 : 0.2
1/6 : 0.16666666666666666
1/7 : 0.14285714285714285
1/8 : 0.125
1/9 : 0.1111111111111111
1/10 : 0.1
1/11 : 0.09090909090909091
1/12 : 0.08333333333333333
1/13 : 0.07692307692307693
1/14 : 0.07142857142857142
1/15 : 0.06666666666666667
1/16 : 0.0625
1/17 : 0.058823529411764705
1/18 : 0.05555555555555555
1/19 : 0.05263157894736842
1/20 : 0.05

Example 4:

The decimal module provides support for arbitrary-precision decimal arithmetic and can be used to obtain more accurate results when working with decimal numbers in Python.

Here is an example of how to use the decimal module to print out the decimal equivalents of 1/2, 1/3, 1/4, …, 1/10:

Python3




import decimal
 
for i in range(2, 11):
    result = decimal.Decimal(1) / decimal.Decimal(i)
    print(f"1/{i}: {result}")


Output

1/2: 0.5
1/3: 0.3333333333333333333333333333
1/4: 0.25
1/5: 0.2
1/6: 0.1666666666666666666666666667
1/7: 0.1428571428571428571428571429
1/8: 0.125
1/9: 0.1111111111111111111111111111
1/10: 0.1

This code imports the decimal module and uses the Decimal class to represent decimal numbers. The Decimal class has a / operator that can be used to perform division with arbitrary precision. The code then uses a for loop to iterate through the range 2 to 10 and prints out the decimal equivalent of 1/i for each value of i.

Using the decimal module can be more computationally expensive than using the built-in float type, but it can be useful for situations where a high degree of precision is required.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads