下限和上限整数的平方

读取下限和上限整数,计算从下限的平方到上限的平方的所有整数的平方和,并显示结果。

#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;
}

原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/266551.html

(0)
上一篇 2022年6月7日
下一篇 2022年6月7日

相关推荐

发表回复

登录后才能评论