ProgIng - Programación en Ingeniería
Loading...
Searching...
No Matches
Ejemplo008.c
Go to the documentation of this file.
1
27
28
#include <stdio.h>
29
36
int
main
(
int
argc,
char
*argv[])
37
{
38
(void)argc;
39
(void)argv;
40
41
float
pi;
/* acumulador para π */
42
int
i, n;
/* i: índice; n: términos */
43
int
den;
/* denominador (2i+1) */
44
int
signo;
/* +1 o -1 alternando */
45
46
/* Validar n >= 1 */
47
do
{
48
printf(
"Ingrese el numero de terminos: "
);
49
scanf(
"%d"
, &n);
50
}
while
(n < 1);
51
52
/*
53
Inicialización:
54
i = 0
55
pi = 0 (acumulador de π/4 antes de multiplicar por 4)
56
*/
57
for
(i = 0, pi = 0; i < n; i++)
58
{
59
den = 2*i + 1;
/* 1, 3, 5, 7, ... */
60
signo = 1 - 2*(i % 2);
/* i par -> +1, i impar -> -1 */
61
pi += (signo * 1.0f / den);
62
}
63
64
pi *= 4;
/* π = 4*(π/4) */
65
printf(
"PI = %f\n"
, pi);
66
67
return
0;
68
}
main
int main(void)
Definition
Ejemplo_035.c:15
src
20261
src
Ejemplo008.c
Generated by
1.16.1