LMS知識社群各系上網率圖資處首頁登入
106/10/17作業(求1+1/2+1/3....+1/n)
by 郭怡勝 2017-10-17 10:46:14, 回應(0), 人氣(191)

程式碼:
/*
 1+1/2+1/3...+1/n
*/
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double sum=0.0;
int n=1,m;
printf("請輸入要求總合:");
scanf("%d", &m);
do
{
  sum=sum+(double)1/n;
  printf("總和= %lf n=%d\n",sum,n);
  n=n+1;
}while(sum< m);
printf("總和= %lf n=%d\n",sum,n-1);
}
附件: