Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given a starting year and an ending year, determine the sum of all of the leap y

ID: 3756263 • Letter: G

Question

Given a starting year and an ending year, determine the sum of all of the leap years that fall
within those boundaries. This problem was given for homework #3 and students had to write it
without functions. If you weren't able to solve it, please consult the solution that is posted. For
this problem, your job is to convert that solution (or your own solution), to one that uses the
following three functions:
// Returns the smallest value x such that x >= start and x is
// a multiple of n.
int getNextMultiple(int start, int n);
// Returns the largest value x such that x <= end and x is
// a multiple of n.
int getPrevMultiple(int end, int n);
// Returns the sum of the arithmetic series that starts at
// start, ends at end and has a common difference of skip.
// A pre-condition is that end >= start and (end-start) is
// divisible by skip.
int sumArithSeries(int start, int end, int skip);
Most of the credit on this problem will be given to implementing these functions as the
comments indicate how they should be implemented.
After writing and testing these three functions, rewrite your main function to solve the problem
by making the appropriate calls to these functions.
Input Specification
Both the starting year, s, and the ending year, e, will be in between 1600 and 10,000, inclusive.
In addition, s e. (So, in full, the restrictions are: 1600 s e 10000.)
Output Specification
Output a single line of the format:
The sum of the leap years in between S and E is C.
where S and E are the starting and ending years inputted by the user and C is the result of the
query.

Explanation / Answer

//Header files

#include<stdio.h>

//Returns the smallest value x such that x >= start and x is

//a multiple of n.

int getNextMultiple(int start, int n)

{

   //If the year is divisible by n, return as it is

   if(start % n == 0)

       return start;

   //Otherwise return next multiple of n

   return start + n - start % n;

}

//Returns the largest value x such that x <= end and x is

//a multiple of n.

int getPrevMultiple(int end, int n)

{

   //If the year is divisible by n, return as it is

   if(end % n == 0)

       return end;

   //Otherwise return previous multiple of n

   return end - end % n;

}

//Returns the sum of the arithmetic series that starts at

//start, ends at end and has a common difference of skip.

int sumArithSeries(int start, int end, int skip)

{

   //n is the number of years from start to end which are dividible by skip

   int n = ((end - start) / skip) + 1;

   //Sum of arithmetic series = (First term + Last term) * (Number of terms / 2)

   return ((start + end) * n) / 2;

}

//main() begins

int main()

{

   int startYear, endYear;

   //Input starting and ending year

   printf("Enter the start and end year: ");

   scanf("%d%d", &startYear, &endYear);

   //start = startYear if start is leap year

   //Otherwise, start = First leap year after startYear

   int start4 = getNextMultiple(startYear, 4);

   //end = endYear if end is leap year

   //Otherwise, end = Last leap year before endYear

   int end4 = getPrevMultiple(endYear, 4);

   int skip4 = 4;

   //Call sumArithSeries with appropriate values

   int sum4 = sumArithSeries(start4, end4, skip4);

   int start100 = getNextMultiple(startYear, 100);

   int end100 = getPrevMultiple(endYear, 100);

   int skip100 = 100;

   int sum100 = sumArithSeries(start100, end100, skip100);

   int start400 = getNextMultiple(startYear, 400);

   int end400 = getPrevMultiple(endYear, 400);

   int skip400 = 400;

   int sum400 = sumArithSeries(start400, end400, skip400);

   int result = sum4 - sum100 + sum400;

   printf("The sum of the leap years in between %d and %d is %d.", startYear, endYear, result);

   return 0;

//main() ends

}

Output for some sample test cases is also provided. User input is in bold.

Enter the start and end year: 1998 2018
The sum of the leap years in between 1998 and 2018 is 10040.

Enter the start and end year: 2000 2018
The sum of the leap years in between 2000 and 2018 is 10040.

Enter the start and end year: 2000 3000
The sum of the leap years in between 2000 and 3000 is 607200.

Kindly give a thumbs up, if found useful. Comment for queries. :)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote