Double Precision Pi Approximation - Python Programming Exercise

This Python program calculates an approximation for PI using the series expression: pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ... The program allows the user to specify the number of terms to use, and it displays the intermediate results of the approximation up to that point. By iterating through the sequence of odd numbers and alternating the signs (positive and negative), it builds the approximation step by step. This exercise is a practical demonstration of how infinite series can be used to compute complex mathematical constants like PI through iterative methods. It helps solidify concepts like loops, arithmetic operations, and user input in Python. Calculating PI through this series is a great way to learn about convergence and how many iterations are required to get increasingly precise approximations. The program demonstrates the power of loops to handle iterative calculations and the use of alternating signs to simulate the mathematical behavior of the series. It's a hands-on way of exploring floating-point operations and understanding how algorithms can approximate real-world constants in Python.

 Category

Python Data Types

 Exercise

Double Precision Pi Approximation

 Objective

Develop a Python program to calculate an approximation for PI using the expression:

pi/4 = 1/1 - 1/3 + 1/5 - 1/7 + 1/9 - 1/11 + 1/13 ...

The user will specify the number of terms to be used, and the program will display all the results up to that number of terms.

 Example Python Exercise

 Copy Python Code
# Prompt the user for the number of terms
num_terms = int(input("Enter the number of terms: "))

# Initialize variables
pi_approximation = 0
sign = 1  # This alternates between 1 and -1 for each term

# Loop through the terms to calculate the approximation
for i in range(num_terms):
    term = sign / (2 * i + 1)  # Calculate the current term
    pi_approximation += term  # Add the term to the approximation
    sign *= -1  # Alternate the sign for the next term

# Multiply the approximation by 4 to get the value of pi
pi_approximation *= 4

# Display the result
print(f"Approximated value of PI using {num_terms} terms: {pi_approximation}")

 Output

Case 1:
Enter the number of terms: 5
Approximated value of PI using 5 terms: 3.339682539682539

Case 2:
Enter the number of terms: 10
Approximated value of PI using 10 terms: 3.0418396189294032

Case 3:
Enter the number of terms: 20
Approximated value of PI using 20 terms: 3.121595216925108

 Share this Python Exercise

 More Python Programming Exercises of Python Data Types

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Perimeter and Area

    This Python program calculates the perimeter, area, and diagonal of a rectangle based on its width and height. The program prompts ...

  •  Hex and Bin

    This Python program prompts the user to enter a number and then displays its equivalent values in both hexadecimal and binary formats. The program conti...

  •  Binary Code

    This Python program prompts the user to enter a decimal number and displays its equivalent in binary form. Instead of using the str() function, the prog...

  •  Conditional Logic and Booleans

    This Python program uses the conditional operator to assign a boolean variable named "bothEven" the value "True" if both numbers entered by the user are...

  •  Exception Handling V2

    This Python program prompts the user for a real number and displays its square root. The program uses a "try...except" block to handle any potential err...

  •  Character

    This Python program prompts the user to input three letters and then displays them in reverse order. The program uses basic input and string manipulation techn...