作業(yè)一:累加求和
#include<stdio.h>
int main()
{
int i=1;
int sum=0;
do
{
sum=sum+i;
i++;
}while(i<=100);
printf("1+...+%d=%d\n",i-1,sum);
return 0;
}
作業(yè)二:累加求和(二)
#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;
}
作業(yè)三:累加求和(三)
#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;
}
作業(yè)四:輸入求和
#include <stdio.h>
int main()
{
int sum = 0,count = 0,bool_cin = 1;
while(bool_cin){
int num = 0;
printf("請輸入一個整數(shù)\n");
scanf("%d",&num);
sum += num;
++count;
printf("是否繼續(xù)?yes->1 no->0\n");
scanf("%d",&bool_cin);
}
double ave = (double)sum/count;
printf("和為%d,平均值為%.2g\n",sum,ave);
return 0;
}
作業(yè)五:看結(jié)果寫程序
#include <stdio.h>
int main()
{
int num1 = 0,num2 = 0,sum = 0;
printf("請輸入兩個整數(shù)\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的整數(shù)之和為%d\n",num1,num2,sum);
return 0;
}