-3
I am performing this exercise in C which is a challenge for college. No note is worth. The purpose is to use the for and find pi. The statement is in the body. For some reason, when I give run in the program, it only stops before reading the for And it stays there. There’s no fatal error, nothing. It just doesn’t keep going. I ran it through both Codeblocks and an online compiler to see what the problem was. I took a look at the go’s command a few times, but I couldn’t identify the problem.
The code:
*#include <stdio.h>
#include <math.h>
#include <locale.h>*
int main (){
int i, vezes, n_termos;
float pi;
float soma;
setlocale(LC_ALL, "Portuguese");
printf("Este programa utiliza a fórmula de Leibniz para calcular a constante pi:\n\n");
printf(" 1 1 1 1 1 1 pi \n");
printf(" --- - --- + --- - --- + --- - --- + ... = -----\n");
printf(" 1 3 5 7 9 11 4 \n\n");
printf("Para tal, insira um número de termos a serem considerados. O programa fica \n");
printf(" mais preciso conforme mais termos são inseridos! \n");
printf("\n\n///////////////////////////////////////////////////////////////////////////////\n\n");
printf("NÚMERO DE TERMOS = ");
scanf("%d", &n_termos);
soma = 1;
vezes = 0;
for (vezes; vezes = n_termos - 1; vezes + 1){
if (vezes % 2 != 0){
i = (3 * vezes) + 2;
soma = soma - (1/i);}
if (vezes % 2 == 0){
i = (3 * vezes) + 2;
soma = soma + (1/i);}
}
pi = 4 * soma;
printf("\nA constante pi é aproximadamente %f\n", pi);
return 0;
}
Anyone have any suggestions what to do? Thank you!
vezes = n_termos - 1
returnsn_termos - 1
, which is true unlessn_termos
is equal to 1. Replace withvezes < n_termos
. Andvezes + 1
, replace withvezes++
.– Vander Santos
@Vandersantos made the replacement, thank you!
– trevchan