Value of Pi(Π) up to 50 decimal places

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 = 4;
  
    // Function that prints
    // the value of pi
    printValueOfPi(N);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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 
    pi = round(2 * acos(0.0), N); 
      
    # Print value of pi upto 
    # N decimal places 
    print(pi); 
  
# Driver Code 
if __name__ == "__main__"
  
    N = 4
  
    # Function that prints 
    # the value of pi 
    printValueOfPi(N); 
      
# This code is contributed by AnkitRai01

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output:

3.1416

Time Complexity: O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : AnkitRai01

Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.