Question

Write a program that approximates the value of pi. For a given n, the value of...

Write a program that approximates the value of pi. For a given n, the value of pi can be approximated by summing the terms in this series: 4 1 − 4 3 + 4 5 − 4 7 + 4 9 − 4 11 . . . .or by using the following formula:

p i = 4 ∑ k = 1 n ( − 1 ) k + 1 2 k + 1

Assume the value of n is provided by a user.

Homework Answers

Answer #1
# pi_value.py

# most accurate value is at value n = 1000 
n = int(input("Enter value of n : "))

sum = 0
odd = 1

for i in range(n):

    # as we need to perform alternate
    # subtractions and addition
    # we consider even index for addition
    # odd index for subtraction
    if i%2==0:
        sum += (4/odd)
    else:
        sum -= (4/odd)
    
    odd += 2

    
print("Value of  pi : ",sum)

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a C program that prompts the user to input as many unsigned(n) as the user...
Write a C program that prompts the user to input as many unsigned(n) as the user wants to type, terminated by non negative number You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a double You may assume without checking that when the user is supposed to type an unsigned, he nevertypes a negative number. For each number the user types, output whether that number is prime or not. You won't need...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop...
IN C++ AS SIMPLE AS POSSIBLE ______ Re-write the given function, printSeriesSquareFifth,  to use a while loop (instead of for). • The function takes a single integer n as a parameter • The function prints a series between 1 and that parameter, and also prints its result • The result is calculated by summing the numbers between 1 and n (inclusive). If a number is divisible by 5, its square gets added to the result instead. • The function does not...
Write a program to find all the numbers divisible by 7 between a given range of...
Write a program to find all the numbers divisible by 7 between a given range of numbers [n1, n2] entered by the user using a FOR loop only. write C program. help
write a java code. Write a program using loops to compute the sum of the 30...
write a java code. Write a program using loops to compute the sum of the 30 terms of the series below. 91/(3 + 2 + 2) + 92/(4 - 4 + 5) + 93/(5 + 6 - 8) + 94/(6 - 8 + 11) + 95/(7 + 10 + 14) + 96/(8 - 12 - 17) + . . . . Output: Term Ratio Sum 1 13 13 2 18.4 31.4 etc etc
Answer the questions based on the program given below. a) Write the function prototype of fnFunny...
Answer the questions based on the program given below. a) Write the function prototype of fnFunny function. b) Write the output produced by the above program #include <stdio.h> int main (void){       unsigned short i =4;       unsigned long j = 6 ;       short k = 2 ;       k = fnFunny ( i , j );       printf("i = %hu, j = %lu, k = %hi\n", i, j, k);       return 0; } short fnFunny (unsigned short i,...
how can I write pi as a maclaurin series using the maclaurin series of arctan(x) and...
how can I write pi as a maclaurin series using the maclaurin series of arctan(x) and letting x = 1 / sqrt(3)?
Write a program that asks the user of a positive integer value. The program should use...
Write a program that asks the user of a positive integer value. The program should use a loop to get the sum of all the integers from 1 up to the number entered. For example, if the user enters 50, the loop will find the sum of 1,2,3,4,…,50. using namespace std;
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2....
Write a program display the following menu: Ohms Law Calculator 1. Calculate Resistance in Ohms 2. Calculate Current in Amps 3. Calculate Voltage in volts 4. Quit Enter your choice (1-4) If the user enters 1, the program should ask for voltage in Volts and the current in Amps. Use the following formula: R= E/i Where: E= voltage in volts I= current in amps R= resistance in ohms If the user enters 2 the program should ask for the voltage...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file...
Write a C++ program to demonstrate thread synchronization. Your main function should first create a file called synch.txt. Then it will create two separate threads, Thread-A and Thread-B. Both threads will open synch.txt and write to it. Thread-A will write the numbers 1 - 26 twenty times in nested for loops then exit. In other words, print 1 - 26 over and over again on separate lines for at least 20 times. Thread-B will write the letters A - Z...
Given the alternating series:    n=2∞(-1)^n/ln(n) (7 pts) Determine if the series converge absolutely.    (Use the fact...
Given the alternating series:    n=2∞(-1)^n/ln(n) (7 pts) Determine if the series converge absolutely.    (Use the fact that: ln n < n ) (7 pts) Determine if the series converge conditionally. (7 pts) Estimate the sum of the infinite series using the first 4 terms in the series and estimate the error. (7 pts) How many terms should we use to approximate the sum of the infinite series in question, if we want the error to be less than 0.5?