Value of Pi(Π) up to 50 decimal places

10 Jun, 2021
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.14Input: 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++
Java
Python3
C#
Javascript
// 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 placesvoid 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 Codeint main(){    int N = 45;     // Function that prints    // the value of pi    printValueOfPi(N);    return 0;}
// Java program to calculate the// value of pi up to n decimal placesclass 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 program to calculate the# value of pi up to n decimal placesfrom math import acos # Function that prints the# value of pi upto N# decimal placesdef 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 Codeif __name__ == "__main__" :     N = 43;     # Function that prints    # the value of pi    printValueOfPi(N);     # This code is contributed by AnkitRai01
// C# program to calculate the// value of pi up to n decimal placesusing 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
<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>
3.1416
 
Time Complexity: O(1)
ankthon
vani39
divyesh072019
C Programs
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Header files in C/C++ and its uses
C Program to read contents of Whole File
How to return multiple values from a function in C or C++?
C++ Program to check Prime Number
Producer Consumer Problem in C
Program for Fibonacci numbers
Set in C++ Standard Template Library (STL)
Write a program to print all permutations of a given string
C++ Data Types
Merge two sorted arrays