-
Lenguaje
Java usando BufferedReader
-
Descripción
Calcular el valor de π con N términos la siguiente serie:
π = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ...
Tenga en cuenta que mientras más términos genere más se acerca al valor de π.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import java.io.*;
public class ValorDePi {
public static void main(String[] args) throws IOException {
int i, n;
double pi;
pi = 0;
System.out.print("Ingresa el valor de n: ");
n = Integer.parseInt(in.readLine());
for (i=1; i<=n; i++) {
System.out.print("PROCESO " + i);
pi=pi+4.0/(i*2-1);
System.out.println();
}
System.out.println("Valor de pi: " + pi);
in.close();
}
}
public class ValorDePi {
public static void main(String[] args) throws IOException {
int i, n;
double pi;
pi = 0;
System.out.print("Ingresa el valor de n: ");
n = Integer.parseInt(in.readLine());
for (i=1; i<=n; i++) {
System.out.print("PROCESO " + i);
pi=pi+4.0/(i*2-1);
System.out.println();
}
System.out.println("Valor de pi: " + pi);
in.close();
}
}