累加求和
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("請(qǐng)輸入一個(gè)整數(shù)\n");
7 scanf("%d",&num);
8 sum += num;
9 ++count;
10 printf("是否繼續(xù)?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 }
看結(jié)果寫程序
1 #include <stdio.h>
2 int main(){
3 int num1 = 0,num2 = 0,sum = 0;
4 printf("請(qǐng)輸入兩個(gè)整數(shù)\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的整數(shù)之和為%d\n",num1,num2,sum);
16 return 0;
17 }