Переписать код на СИ из for у while и do while
Можете переписать код из for у while и do while? Я пыталась сделать но у меня пропускается q1 и другие значения. Вот мой for
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int t,fact=1,k;
float y = 0,x,sum = 0;
printf("\nEnter x= ");
scanf("%f",&x);
if ((x>-2)&&(x<2)) {
for(k=1;k>0;k++) {
for(t=1;t<=k+1;t++){
fact=fact*t;
}
sum=(pow(-1,k)*pow(x,2*k+1))/((2*k-1)*fact);
printf ("\n q[%d]=%f",k,sum);
if (fabs(sum)<0.0001) {
printf ("\nbreaks on k=%d",k);
break;
}
y=y+sum;
}
printf("\nThe sum of the members of the series=%f",y);
printf("\nNumber of terms:%d",k);
}
else printf("\nError: you can enter -2<x<2");
return 0;
}
Вот мой while который не получился
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int t = 1,fact=1;
float y,x,sum, k=1;
printf("\nEnter x= ");
scanf("%f",&x);
if ((x>-2)&&(x<2)) {
while(k>0) {
k++;
while(t<=k+1){
t++;
fact=fact*t;
}
sum=(pow(-1,k)*pow(x,2*k+1))/((2*k-1)*fact);
printf ("\n q[%f]=%f",k,sum);
if (fabs(sum)<0.0001) {
printf ("\nbreaks on k=%d",k);
break;
}
y=y+sum;
}
printf("\nThe sum of the members of the series=%f",y);
printf("\nNumber of terms:%d",k);
}
else printf("\nError: you can enter -2<x<2");
return 0;
}
Ответы (1 шт):
Автор решения: Igor
→ Ссылка
while (true) {
//k++;
t = 1;
while (t <= k + 1) {
fact = fact * t;
t++;
}
...
k++;
}