while语句作业

47 阅读1分钟

作业1:

s=1+3+5+7+...+99

#include <stdio.h>

int main() {
    int s = 0;
    int i = 1;
    while (i <= 99) {
        printf("%d \n", i);
        s += i;
        i +=2;
    }
    printf("%d \n", s);
    return 0;
}


编译

image.png

作业2:

s=2+4+6+8+...+100

#include <stdio.h>

int main() {
    int s = 0;
    int i = 2;
    while (i <=100) {
        printf("%d \n", i);
        s += i;
        i+=2;
    }
    printf("%d \n", s);
    return 0;
}


编译

image.png