编程学习网为您整理以下代码实例,主要实现:下限和上限整数的平方,希望可以帮到各位朋友。
读取下限和上限整数,计算从下限的平方到上限的平方的所有整数的平方和,并显示结果。
#include <stdio.h>
int sum_of_squares(int lower, int upper);
int main(voID)
{
int upper, lower, reads;
printf("Enter lower and upper integer limits: ");
while(reads = scanf("%d%d", &lower, &upper), reads == 2 && lower < upper){
printf("The sums of the squares from %d to %d is %d\n",lower * lower, upper * upper, sum_of_squares(lower, upper));
printf("Enter next set of limits: ");
}//
printf("Done\n");
return 0;
}
int sum_of_squares(int lower, int upper) // calculate sum of squares from lower to upper
{
int sum = 0;
for (int i = lower; i <= upper; i++)
{
sum += i * i;
}
return sum;
}
本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!