Read lower and upper integer limits, calculates the sum of all the integer squares from the square of the lower limit to the square of the upper limit, and displays the answer. - C Data Type

C examples for Data Type:int

Description

Read lower and upper integer limits, calculates the sum of all the integer squares from the square of the lower limit to the square of the upper limit, and displays the answer.

Demo Code

#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: ");
  }//from  w ww  . j a  v  a 2  s . co  m
  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;
}

Result


Related Tutorials