/*编写一个程序,要求用户输入一个上限整数和一个下限整数,计算从上限到下限范围内所有整数的平方和,并显示
计算结果。然后程序继续提示用户输入上限和下限整数,并显示结果,直到用户输入的上限整数等于或者小于下限整数为止。
程序的运行示例如下:
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(void)
{
int lower;
int upper;
printf("Enter lower and upper integer limits:");
scanf("%d%d", &lower, &upper);
while (lower < upper) {
int square_lower = lower * lower;
int square_upper = upper * upper;
int sums=0;
int i;
int j = lower;
for (i = 0; i < upper-lower + 1; i++) {
sums += j * j;
j++;
}
printf("The sums of squares from %d to %d is %d\n", square_lower, square_upper, sums);
printf("Enter next set of limits:");
scanf("%d%d", &lower, &upper);
}
return 0;
}
C PRIMER PLUS(第六版编程练习)6.16编程练习_10题
最新推荐文章于 2022-05-14 13:28:38 发布