Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace FindPi
- {
- class Program
- {
- static void Main(string[] args)
- {
- double pi = 4; //Create the variable for Pi and set its start value to 4.
- int denominator = 3; //The variable for the denominator in the Pi formula.
- int counter = 3; //Where are we in Pi.
- int termNumber = 2; //Keep track of the term number to display to the user.
- ///Next chunk of code asks the user how many terms they want.
- Console.WriteLine("Please enter the number of terms:");
- string termsNumberString = Console.ReadLine();
- int termsNumber;
- termsNumber = Convert.ToInt32(termsNumberString);
- termsNumber = termsNumber * 2;
- ///Display the table headers.
- string titleCol1 = ("Term");
- string titleCol2 = ("Pi");
- string formatTitleTable = "{0,-15} {1,-10}";
- Console.WriteLine(formatTitleTable, titleCol1, titleCol2);
- ///Displaying the first term. This is done maually rather than using the formula because it's a starting point.
- string term1Col1 = ("1");
- string term1Col2 = ("4.0000000000000000");
- string formatTerm1Table = "{0,-15} {1,-10}";
- Console.WriteLine(formatTerm1Table, term1Col1, term1Col2);
- ///Figuring out the term value for Pi.
- while (denominator <= termsNumber)
- {
- if (counter % 2 != 0)
- {
- pi -= Convert.ToDouble(4) / denominator;
- counter++;
- }
- else
- {
- pi += Convert.ToDouble(4) / denominator;
- counter++;
- }
- denominator += 2;
- ///Displaying the term number and the value of Pi to the user in a table format.
- string col1 = ("" + termNumber);
- termNumber++;
- string col2 = ("" + pi);
- string formatTable = "{0,-15} {1,-10}";
- Console.WriteLine(formatTable, col1, col2);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement