#include <stdio.h>
#include <stdlib.h>
double calcSoma(int n);
long int fatorial(int n);
int main () {
int termos;
do {
printf("Digite a qtd de termos (>= 5): "); }while(termos < 5);
printf("\nO somatorio para %d termos eh %e", termos
, calcSoma
(termos
));
}
double calcSoma (int n){
double s = 1;
int i, numerador = 0, valFat = 1;
for (i = 0; i < n; i ++) {
numerador = numerador + 2;
valFat = valFat + 2; //numeros impares
s = s + (double)numerador/fatorial(valFat);
}
return s;
}
long int fatorial(int n){
long int fat = 0;
if (n == 0) {
return 1;
}else {
fat = n * (fatorial(n-1));
}
return fat;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCmRvdWJsZSBjYWxjU29tYShpbnQgbik7CmxvbmcgaW50IGZhdG9yaWFsKGludCBuKTsKCmludCBtYWluICgpIHsKICAgIGludCB0ZXJtb3M7CmRvIHsKICAgIHByaW50ZigiRGlnaXRlIGEgcXRkIGRlIHRlcm1vcyAoPj0gNSk6ICIpOwogICAgc2NhbmYoIiVkIiwgJnRlcm1vcyk7Cn13aGlsZSh0ZXJtb3MgPCA1KTsKIHByaW50ZigiXG5PIHNvbWF0b3JpbyBwYXJhICVkIHRlcm1vcyBlaCAlZSIsIHRlcm1vcywgY2FsY1NvbWEodGVybW9zKSk7Cgp9Cgpkb3VibGUgY2FsY1NvbWEgKGludCBuKXsKICAgIGRvdWJsZSBzID0gMTsKICAgIGludCBpLCBudW1lcmFkb3IgPSAwLCB2YWxGYXQgPSAxOwogICAgZm9yIChpID0gMDsgaSA8IG47IGkgKyspIHsKICAgICAgICBudW1lcmFkb3IgPSBudW1lcmFkb3IgKyAyOwogICAgICAgIHZhbEZhdCA9IHZhbEZhdCArIDI7IC8vbnVtZXJvcyBpbXBhcmVzCiAgICAgICAgcyA9IHMgKyAoZG91YmxlKW51bWVyYWRvci9mYXRvcmlhbCh2YWxGYXQpOwogICAgfQpyZXR1cm4gczsKfQoKbG9uZyBpbnQgZmF0b3JpYWwoaW50IG4pewogICAgbG9uZyBpbnQgZmF0ID0gMDsKICAgIGlmIChuID09IDApIHsKICAgICAgICByZXR1cm4gMTsKICAgIH1lbHNlIHsKICAgICAgICBmYXQgPSBuICogKGZhdG9yaWFsKG4tMSkpOwogICAgfQogICAgcmV0dXJuIGZhdDsKfQ==