累加求和
1 #include <stdio.h>
2 int main(){
3 int i = 1;
4 int sum = 0;
5 do{
6 sum += i;
7 ++i;
8 }while(i != 101);
9 printf("总和为:%d\n",sum);
10 return 0;
11 }
累加求和(二)
1 #include <stdio.h>
2 int main(){
3 int i = 1;
4 int sum = 0;
5 do{
6 if(i%2 != 0){
7 sum += i;
8 }
9 ++i;
10 }while(i != 101);
11 printf("总和为:%d\n",sum);
12 return 0;
13 }
累加求和(三)
1 #include <stdio.h>
2 int main(){
3 int i = 1;
4 int sum = 0;
5 do{
6 if(i%2 == 0){
7 sum += i;
8 }
9 ++i;
10 }while(i != 101);
11 printf("总和为:%d\n",sum);
12 return 0;
13 }
输入求和
1 #include <stdio.h>
2 int main(){
3 int sum = 0,count = 0,bool_cin = 1;
4 while(bool_cin){
5 int num = 0;
6 printf("请输入一个整数\n");
7 scanf("%d",&num);
8 sum += num;
9 ++count;
10 printf("是否继续?yes->1 no->0\n");
11 scanf("%d",&bool_cin);
12 }
13 double ave = (double)sum/count;
14 printf("和为%d,平均值为%.2g\n",sum,ave);
15 return 0;
16 }
看结果写程序
1 #include <stdio.h>
2 int main(){
3 int num1 = 0,num2 = 0,sum = 0;
4 printf("请输入两个整数\n");
5 scanf("%d%d",&num1,&num2);
6 if(num1 > num2){
7 num1 += num2;
8 num2 = num1-num2;
9 num1 -= num2;
10 }
11 int i = 0;
12 for(i = num1;i <= num2;i++){
13 sum += i;
14 }
15 printf("大于等于%d小于等于%d的整数之和为%d\n",num1,num2,sum);
16 return 0;
17 }