Open In App

Value of Pi(Π) up to 50 decimal places

Last Updated : 10 Oct, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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




import java.io.*;
import java.text.DecimalFormat;
// 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);
        pi = Math.round(pi * Math.pow(10, n))
             / Math.pow(10, n);
         
          // Print value of pi upto
        // N decimal places
        System.out.println(pi);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int N = 7;
 
        // 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)
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads