3 喜歡 0 不喜歡
65 瀏覽

參考以下程式的架構,分別計算 1 到 10000 之間,所有 2 的倍數、3 的倍數與 5 的倍數的數值總和。

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
    int ix = 1;
    int iEven, iOdd; 
    iEven = iOdd = 0; /* 如果有多個變數要設定成相同內容, 可以寫成這樣 */
    while( ix <= 100 ) 
    {    /* 利用偶數除 2 一定餘 0, 奇數除 2 一定餘 1 的性質來判斷奇,偶數 */
        if( ix%2 == 0 )  iEven += ix; // 也就是:iEven = iEven + ix;
        if( ix%2 == 1 )  iOdd  += ix; // 也就是:iOdd = iOdd + ix;
        ix++; /* ix = ix + 1 */
    }
    printf("偶數: 2+4...+100=%4d\n",iEven);
    printf("奇數: 1+3...+ 99=%4d\n",iOdd);
    system("pause"); return 0;
}

 

參考書籍:旗標無痛學習教本

[正常] 開放式問答 - 最新提問 分類:Introduction to Computer Programming I (C) | 用戶: (20.9k 分)
ID: 21817 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 65 瀏覽
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.70.39.120
©2016-2024

相關問題

3 喜歡 0 不喜歡
0 回答
[正常] 開放式問答 - 最新提問 1月 25, 2017 分類:Introduction to Computer Programming I (C) | 用戶: The Contributor (20.9k 分)
ID: 21820 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 97 瀏覽
3 喜歡 0 不喜歡
0 回答
[正常] 開放式問答 - 最新提問 1月 25, 2017 分類:Introduction to Computer Programming I (C) | 用戶: The Contributor (20.9k 分)
ID: 21819 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 55 瀏覽
3 喜歡 0 不喜歡
0 回答
[正常] 開放式問答 - 最新提問 1月 25, 2017 分類:Introduction to Computer Programming I (C) | 用戶: The Contributor (20.9k 分)
ID: 21818 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 73 瀏覽
2 喜歡 0 不喜歡
0 回答
[正常] 開放式問答 - 最新提問 2月 11, 2017 分類:Introduction to Computer Programming I (C) | 用戶: The Contributor (20.9k 分)
ID: 21896 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 120 瀏覽
3 喜歡 0 不喜歡
0 回答
[正常] 開放式問答 - 最新提問 2月 12, 2017 分類:Introduction to Computer Programming I (C) | 用戶: The Contributor (20.9k 分)
ID: 21923 - 從幾時開始: 無限制 - 到幾時結束: 無限制
| 111 瀏覽
12,783 問題
183,443 回答
172,219 留言
4,824 用戶