题目:编写一个程序, 要求用户输入一个上限整数和一个下限整数, 计算从上限到下限范围内所有整数的平方和, 并显示计算结果。 然后程序继续提示用户输入上限和下限整数, 并显示结果, 直到用户输入的上限整数小于下限整数为止。 程序的运行示例如下:
Enter lower and upper integer limits: 5 9
The sums of the squares from 25 to 81 is 255
Enter next set of limits: 3 25
The sums of the squares from 9 to 625 is 5520
Enter next set of limits: 5 5
Done
#include<stdio.h>
int main()
{
int a,b;
long int c=0;
printf("Enter lower and upper integer limits:");
while(scanf("%d %d",&a,&b)==2 && a<b)
{
printf("The sums of the squares from %d to %d is ",a*a,b*b);
for(c=0; a<=b; a++) //a值仍取scanf函数输入值,不进行操作
c+=(long)a*a; //每次新循环需将c赋0,否则会叠加上一次计算的c值
printf("%ld",c);
printf("\nEnter next set of limits:");
}
printf("Done ");
return 0;
}
运行结果:以5 9、3 25 和 5 5为例
备注:自己写的,也可能有问题,大家一起交流呀