“21天养成好习惯”第一期-13
今天学习了如何计算整数段和
#include<stdio.h>
int main(){
int A,B,Sum=0;
int num=0;
scanf("%d %d",&A,&B);
for(int i=A;i<=B;i++)
{
Sum=Sum+i;
num++;
printf("%5d",i);
if(num%5==0&&i!=B)
printf("\n");
}
printf("\n");
printf("Sum = %d",Sum);
return 0;
}
运行结果如下:
-3 8
-3 -2 -1 0 1
2 3 4 5 6
7 8
Sum = 30
--------------------------------
Process exited after 1.707 seconds with return value 0
请按任意键继续. . .