Coronavirus made it tough but we keep working remotely with no delays. Get 15% OFF your First Order
Get 15% OFF your First Order


Exercise 3 C#

Help me study for my Computer Science class. I’m stuck and don’t understand.

Assignment: Exercise 3

Console App: π (PI) Calculator using Leibniz formula

Needs to be done in Visual studio 2019


The objectives of this lab assignment are as follows:

  • Seek a value that can be calculated only by adding the successive values returned by a mathematic formula performed in numerous iterations of a loop
  • Output information conditionally as a loop continues to function

Skills Required

To properly complete this assignment you will need to apply the following skills:

  • Convert a math formula of sufficient complexity to C# code
  • Use a loop to perform a mathematic calculation summing the results from each iteration
  • Output results at various intervals during the loop’s execution

Assignment User Story

As a user I want an application that will allow me to use the Gregory/Leibniz formula for calculating the value of PI. Further, because the accuracy of this formula depends on the number of iterations I wish to know the value of PI as calculated by this formula after a number of set intervals: 10; 1,000; 100,000; 500,000; 1,000,000; and a user entered upper value that is greater than 1 million.

Assignment Requirements

The number π is a mathematical constant, the ratio of a circle’s circumference to its diameter, commonly approximated as 3.14159. It has been represented by the Greek letter “π” since the mid-18th century, though it is also sometimes spelled out as “pi.” Being an irrational number, π cannot be expressed exactly as a fraction (equivalently, its decimal representation never ends and never settles into a permanent repeating pattern).

In this application we are calculating the value of PI using a formula that dates from the 17th century. Specifically, we will use the Leibniz formula. This formula is a convergence sequence, which means that its partial sums tends to a limit; this means that the partial sums become closer and closer to a given number when the number of their terms increases. In short, the more times we perform this calculation we converge toward a more accurate value of the number pi.

In this assignment you will construct a loop to perform the Leibniz formula at least 1 million times. And because the calculated value of pi will vary based on the number of times we perform this sequence I want to know what that value is at certain intervals of the loop. Specifically, you will print the calculated value of pi at the following loop intervals:

  • 10
  • 1,000
  • 100,000
  • 500,000
  • 1,000,000
  • At the end of the loop determined by the value entered by the user.

I do not want separate loops for each of these outputs. Your application should have a single loop that prints the value of pi at the above given iteration values.

Gregory/Leibniz Convergence Formula for Calculating PI

Either in a quest for precision or as mere feats of record-setting there seems to be a never-ending desire to calculate the value of π to the greatest number of digits possible. In fact, approximations for the value of π can be found in ancient Egypt, China, and India.

One formula to calculate π, developed by Gregory (1638-1675) and Leibniz (1646-1716), can be expressed as follows:

This formula represents a simple, infinite series, and is represented in summation notation.

Summation Notation Description

Using this diagram as a guide to the Leibniz formula we understand that the formula to the right of the summation symbol is to be performed by substituting the index value into the formula. Furthermore, the number at the bottom of the symbol specifies the first number to be used while the value at the top of the symbol specifies when we should stop. In the Leibniz formula the upper bounds is infinity, thus we can choose to stop as we please.

Therefore, the first ten iterations of our formula must be as follows:

You will notice that the formula is a simple recurrence. And as the individual terms of this series are added to the sum the total gradually gets closer to π. However, this convergence sequence is inefficient as after 500,000 terms it produces only five correct decimal digits of π.

You should be able to see how each of these calculations should be performed in a loop. Within the loop, we need to keep a sum of the value of this sequence.

Our next step will be to find the value of PI at any given time. This is done by realizing that the sum of the above sequence is equal to . We need simply now to solve for . This means that equals 4 X the sum of the values from our sequence.


Technical/Grading Requirements

You must ensure that the application you submit meets all technical/grading requirements. Your grade will be based on how well your application follows the application requirements plus the following:

  • The application will have an appropriate title
  • The user will be prompted for the number of times the calculation will iterate. Your program will validate that the user has entered a valid number and that the number is greater than 1 million.
  • Using a single loop, you will calculate the value of pi using the Leibniz formula and will output the value when the loop’s index is 10, 1000, 100000, 500000, 1000000, and at the end of the loop.
  • The loop will end at the count provided by the user when the program started.
  • In properly formatting your output the value of pi must be printed to 10 decimals while the iteration value must include the comma separators.

How the application looks and how it presents its information to the user is up to you. But it must meet the minimum requirements noted above.

Example Output

The example output provided below is simply an example. You do not need to format your output to look like this example. You simply need to ensure that you have met the technical requirements for this application.

Note: In the program below I entered three values that were invalid, but I prevent the application from ending in an execution error, rather, I simply re-prompt the user for a valid entry.

Following is example output for this application:

Example Program Output

Looking for this or a Similar Assignment? Click below to Place your Order

× How can I help you?