Double, Approximation Of Pi - C# Programming Exercise

In this C# exercise, you are asked to write a program that calculates an approximation for PI using the alternating fraction series:

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

The program should prompt the user to specify how many terms to use in the approximation and then display the accumulated results up to that number of terms. This exercise is excellent for understanding how mathematical series can be used to approximate values like PI in programming.

Using a loop, the program will iterate through the terms of the series, adding or subtracting terms based on the alternating pattern (positive and negative). At the end, the program will display the approximate value of PI after calculating the specified number of terms. This exercise will also allow you to practice working with loops, flow control, and basic mathematical operations in C#.

Additionally, you will learn how the precision of the approximation improves as the number of terms increases, helping you understand the concepts behind approximations in mathematics and their implementation in programming.

 Category

Basic Data Types

 Exercise

Double, Approximation Of Pi

 Objective

Write a C# 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 indicate how many terms must be used, and the program will display all the results until that amount of terms.

 Write Your C# Exercise

using System;  // Import the System namespace to use basic classes like Console

class Program  // Define the main class of the program
{
    static void Main()  // The entry point of the program
    {
        // Prompt the user for the number of terms
        Console.Write("Enter the number of terms to approximate Pi: ");
        int terms = int.Parse(Console.ReadLine());  // Read the user's input and convert it to an integer

        double piApproximation = 0.0;  // Variable to store the approximation of Pi

        // Loop through each term, adding or subtracting based on the formula
        for (int i = 0; i < terms; i++)  // Loop through the number of terms
        {
            // Calculate the current denominator (odd numbers: 1, 3, 5, 7, 9, ...)
            double denominator = 2 * i + 1;  
            
            // Use the formula to alternate adding and subtracting terms
            if (i % 2 == 0)  // If i is even, add the term
            {
                piApproximation += 1 / denominator;
            }
            else  // If i is odd, subtract the term
            {
                piApproximation -= 1 / denominator;
            }

            // Display the current approximation of Pi (multiply by 4 to get the approximation of Pi)
            Console.WriteLine($"After {i + 1} terms, approximation of Pi: {4 * piApproximation}");
        }
    }
}

 Share this C# exercise

 More C# Programming Exercises of Basic Data Types

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. 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 C#.

  •  Perimeter Area

    In this C# exercise, you are asked to write a program that calculates the perimeter, area, and diagonal of a rectangle from its width and height....

  •  Hexadecimal and binary

    In this C# exercise, you are asked to write a program that prompts the user for a number and displays it in both hexadecimal and binary. The program should continue r...

  •  Binary

    In this C# exercise, you are asked to write a program that prompts the user for a decimal number and displays its equivalent in binary. The program should continue re...

  •  Conditional and boolean

    In this C# exercise, you are asked to write a program that uses the conditional operator (also known as the ternary operator) to assign a boolean variable named "bothEven...

  •  Exceptions V2

    In this C# exercise, you are asked to write a program that prompts the user for a real number and displays its square root. Additionally, the program must handle any ...

  •  Char

    This exercise in C# aims to develop a program that asks the user for three letters and displays them in reverse order. The user will input one letter at...