Skip to content
Related Articles

Related Articles

Improve Article

Value of Pi(Π) up to 50 decimal places

  • Last Updated : 10 Jun, 2021
Geek Week

Given a number N(where N <= 50), the task is to find the value of Pi (Π) up to N decimals places.
Examples: 

Input: N = 2 
Output: 3.14
Input: N = 10 
Output: 3.1415926536  

Approach:  

1. The value of Π is calculated using acos() function which returns a numeric value between [-Π, Π].

2. Since using acos(0.0) will return the value for 2*Π. Therefore to get the value of Π



double pi = 2*acos(0.0);

3. Now the value obtained from above equation is estimated to N decimal digit as:

printf("%.*lf\n", N, pi);

Below is the implementation of the above approach:  

C++




// C++ program to calculate the
// value of pi up to n decimal
// places
#include "bits/stdc++.h"
using namespace std;
 
// Function that prints the
// value of pi upto N
// decimal places
void printValueOfPi(int N)
{
 
    // Find value of pi upto
    // using acos() function
    double pi = 2 * acos(0.0);
 
    // Print value of pi upto
    // N decimal places
    printf("%.*lf\n", N, pi);
}
 
// Driver Code
int main()
{
    int N = 45;
 
    // Function that prints
    // the value of pi
    printValueOfPi(N);
    return 0;
}

Java




// Java program to calculate the
// value of pi up to n decimal places
class GFG {
     
    // Function that prints the
    // value of pi upto N
    // decimal places
    static void printValueOfPi(int N)
    {
     
        // Find value of pi upto
        // using acos() function
        double pi = 2 * Math.acos(0.0);
     
        // Print value of pi upto
        // N decimal places
        System.out.println(pi);
    }
     
    // Driver Code
    public static void main (String[] args)
    {
        int N = 4;
     
        // Function that prints
        // the value of pi
        printValueOfPi(N);
    }
}
 
// This code is contributed by AnkitRai01

Python3




# Python3 program to calculate the
# value of pi up to n decimal places
from math import acos
 
# Function that prints the
# value of pi upto N
# decimal places
def printValueOfPi(N) :
 
    # Find value of pi upto
    # using acos() function
    b = '{:.' + str(N) + 'f}'
    pi= b.format(2 * acos(0.0))
      
     
    # Print value of pi upto
    # N decimal places
    print(pi);
 
# Driver Code
if __name__ == "__main__" :
 
    N = 43;
 
    # Function that prints
    # the value of pi
    printValueOfPi(N);
     
# This code is contributed by AnkitRai01

C#




// C# program to calculate the
// value of pi up to n decimal places
using System;
 
class GFG {
     
    // Function that prints the
    // value of pi upto N
    // decimal places
    static void printValueOfPi(int N)
    {
     
        // Find value of pi upto
        // using acos() function
        double pi = 2 * Math.Acos(0.0);
     
        // Print value of pi upto
        // N decimal places
        Console.WriteLine(pi);
    }
     
    // Driver Code
    public static void Main()
    {
        int N = 4;
     
        // Function that prints
        // the value of pi
        printValueOfPi(N);
    }
}
 
// This code is contributed by AnkitRai01

Javascript




<script>
    // Javascript program to calculate the
    // value of pi up to n decimal places
     
    // Function that prints the
    // value of pi upto N
    // decimal places
    function printValueOfPi(N)
    {
      
        // Find value of pi upto
        // using acos() function
        let pi = 2 * Math.acos(0.0);
      
        // Print value of pi upto
        // N decimal places
        document.write(pi.toFixed(4));
    }
     
    let N = 4;
      
    // Function that prints
    // the value of pi
    printValueOfPi(N);
 
// This code is contributed by divyesh072019.
</script>
Output: 
3.1416

 

Time Complexity: O(1)

We provide nothing but the best curated videos and practice problems for our students. Check out the C Foundation Course and master the C language from basic to advanced level. Wait no more, start learning today!



My Personal Notes arrow_drop_up
Recommended Articles
Page :