Value of Pi(Π) up to 50 decimal places
Last Updated :
10 Oct, 2022
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++
#include "bits/stdc++.h"
using namespace std;
void printValueOfPi( int N)
{
double pi = 2 * acos (0.0);
printf ( "%.*lf\n" , N, pi);
}
int main()
{
int N = 45;
printValueOfPi(N);
return 0;
}
|
Java
import java.io.*;
import java.text.DecimalFormat;
class GFG {
static void printValueOfPi( int n)
{
double pi = 2 * Math.acos( 0.0 );
pi = Math.round(pi * Math.pow( 10 , n))
/ Math.pow( 10 , n);
System.out.println(pi);
}
public static void main(String[] args)
{
int N = 7 ;
printValueOfPi(N);
}
}
|
Python3
from math import acos
def printValueOfPi(N) :
b = '{:.' + str (N) + 'f}'
pi = b. format ( 2 * acos( 0.0 ))
print (pi);
if __name__ = = "__main__" :
N = 43 ;
printValueOfPi(N);
|
C#
using System;
class GFG {
static void printValueOfPi( int N)
{
double pi = 2 * Math.Acos(0.0);
Console.WriteLine(pi);
}
public static void Main()
{
int N = 4;
printValueOfPi(N);
}
}
|
Javascript
<script>
function printValueOfPi(N)
{
let pi = 2 * Math.acos(0.0);
document.write(pi.toFixed(4));
}
let N = 4;
printValueOfPi(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...