[英]C program for first seven terms in natural logarithm. I am not getting the right answer, can anyone review the following code
[英]C program for first seven terms of logarithmic series
如果 x 是通過鍵盤輸入的,請編寫一個程序來計算這一系列的前七項的總和。 我將程序編寫為:
#include <stdio.h>
#include<math.h>
int main()
{
float x, i, sum, log_sum;
printf("Enter the value of x : ");
scanf("%f",&x);
sum=0;
for (i=2; i<=7; i++)
{
sum=sum+((1/2)*pow(((x-1)/x), i));
}
log_sum=((x-1)/x)+sum;
printf("\nSum of log series is %f\n",log_sum);
return 0;
}
output 與計算器答案不匹配。 這里可能有什么問題?
在for
循環中,您將所有內容乘以(1/2)
,假設1
和2
是整數,則答案將為0
。 嘗試乘以0.5
,這能解決問題嗎?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.