11-10

作者: 我有只熊 | 来源:发表于2017-11-13 19:19 被阅读0次

    累加求和

    ···

    include <stdio.h>

    int main(){
    int i = 1;
    int sum = 0;
    do{
    if(i%2 != 0){
    sum += i;
    }
    ++i;
    }while(i != 101);
    printf("总和为:%d\n",sum);
    return 0;
    }

    
    累加求和(二)
    
    

    include <stdio.h>

    int main(){
    int i = 1;
    int sum = 0;
    do{
    if(i%2 == 0){
    sum += i;
    }
    ++i;
    }while(i != 101);
    printf("总和为:%d\n",sum);
    return 0;
    }

    
    累加求和(三)
    
    

    include <stdio.h>

    int main(){
    int sum = 0,count = 0,bool_cin = 1;
    while(bool_cin){
    int num = 0;
    printf("请输入一个整数\n");
    scanf("%d",&num);
    sum += num;
    ++count;
    printf("是否继续?yes->1 no->0\n");
    scanf("%d",&bool_cin);
    }
    double ave = (double)sum/count;
    printf("和为%d,平均值为%.2g\n",sum,ave);
    return 0;
    }

    include <stdio.h>

    int main(){
    int sum = 0,count = 0,bool_cin = 1;
    while(bool_cin){
    int num = 0;
    printf("请输入一个整数\n");
    scanf("%d",&num);
    sum += num;
    ++count;
    printf("是否继续?yes->1 no->0\n");
    scanf("%d",&bool_cin);
    }
    double ave = (double)sum/count;
    printf("和为%d,平均值为%.2g\n",sum,ave);
    return 0;
    }

    include <stdio.h>

    int main(){
    int num1 = 0,num2 = 0,sum = 0;
    printf("请输入两个整数\n");
    scanf("%d%d",&num1,&num2);
    if(num1 > num2){
    num1 += num2;
    num2 = num1-num2;
    num1 -= num2;
    }
    int i = 0;
    for(i = num1;i <= num2;i++){
    sum += i;
    }
    printf("大于等于%d小于等于%d的整数之和为%d\n",num1,num2,sum);
    return 0;
    }

    相关文章

      网友评论

          本文标题:11-10

          本文链接:https://www.haomeiwen.com/subject/qfgomxtx.html