3 like 0 dislike

參考以下程式的架構,分別計算 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;
}

 

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

[Normal] Essay (Open question) - asked in Introduction to Computer Programming I (C) by (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 2 views
Get it on Google Play Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:172.69.69.112
©2016-2019

Related questions

3 like 0 dislike
0 answers
[Normal] Essay (Open question) - asked Jan 25, 2017 in Introduction to Computer Programming I (C) by The Contributor (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 2 views
3 like 0 dislike
0 answers
[Normal] Essay (Open question) - asked Jan 25, 2017 in Introduction to Computer Programming I (C) by The Contributor (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 2 views
3 like 0 dislike
0 answers
[Normal] Essay (Open question) - asked Jan 25, 2017 in Introduction to Computer Programming I (C) by The Contributor (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 2 views
2 like 0 dislike
0 answers
[Normal] Essay (Open question) - asked Feb 11, 2017 in Introduction to Computer Programming I (C) by The Contributor (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 2 views
3 like 0 dislike
0 answers
[Normal] Essay (Open question) - asked Feb 12, 2017 in Introduction to Computer Programming I (C) by The Contributor (20.9k points)
Available when: Unlimited - Due to: Unlimited
| 3 views
8,033 questions
60,929 answers
33,033 comments
1,102 users