3 like 0 dislike
165 views

參考以下程式的架構,分別計算 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)
ID: 21817 - Available when: Unlimited - Due to: Unlimited
| 165 views
Welcome to Peer-Interaction Programming Learning System (PIPLS) LTLab, National DongHwa University
English 中文 Tiếng Việt
IP:108.162.216.42
©2016-2024

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)
ID: 21820 - Available when: Unlimited - Due to: Unlimited
| 197 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)
ID: 21819 - Available when: Unlimited - Due to: Unlimited
| 152 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)
ID: 21818 - Available when: Unlimited - Due to: Unlimited
| 158 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)
ID: 21896 - Available when: Unlimited - Due to: Unlimited
| 233 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)
ID: 21923 - Available when: Unlimited - Due to: Unlimited
| 226 views
12,783 questions
183,443 answers
172,219 comments
4,824 users