試寫一程式, 利用巨集的方式,計算下面式子中, 到第n項的 和, n值由鍵盤輸入取得。 1/1+1/2+1/3+...+1/n 大神幫幫忙?

匿 匿名 693 次瀏覽 2 個回答
我要回答

2 個回答

R
Ray #1 樓 2021-09-15
原始程式碼如下
#include
#include
#define macro(n) for(k=0;k int main(void)
{
int k,n;
float sum = 0.0f;
printf("請輸入第n項\n");
scanf("%d", &n);
printf("輸入的n為:%d\n",n);
printf("計算1/1+1/2+1/3+‧‧‧+1/n的輸出結果為\n");
macro(n);
printf("sum = %f\n", sum);
system("pause");
return 0;
}


執行結果如連結
https://ppt.cc/fWVmwx
網頁版線上編譯(online-ide)
https://ppt.cc/fy0pDx
網頁版線上編譯(ideone)
https://ppt.cc/f6Fmfx
原始檔案下載點
https://ppt.cc/f2dX4x
R
Ray #2 樓 2021-09-15

原始程式碼如下
#include <stdio.h>
#include <stdlib.h>
#define macro(n) for(k=0;k<n;k++)sum+=1/float(k+1)
int main(void)
{
    int k,n;
    float sum = 0.0f;
    printf("請輸入第n項\n");
    scanf("%d", &n);
    printf("輸入的n為:%d\n",n);
    printf("計算1/1+1/2+1/3+‧‧‧+1/n的輸出結果為\n");
    macro(n);
    printf("sum = %f\n", sum);
    system("pause");
    return 0;
}


執行結果如連結
https://ppt.cc/fWVmwx
網頁版線上編譯(online-ide)
https://ppt.cc/fy0pDx
網頁版線上編譯(ideone)
https://ppt.cc/f6Fmfx
原始檔案下載點
https://ppt.cc/f2dX4x

我要回答

登入會員後即可回答問題。

免費註冊