In: Computer Science
Make a function that calculates the summation of even numbers in the range.
The function sum takes the two integer parameters and they are used as the range.
We will use default parameters 0 and 100 for each parameter respectively.
As it is not defined in which language it is to be coded, So seeing the constraints I coded it in C++ which is easily understandable abd comments are being added wherever necesary.
CODE:
#include <iostream>
using namespace std;
class evenNumber
{
public:
// Member Function handles all the cases as defined in
problem
void sum(int x=0, int y=100)
{
//add keeps track of addition of even numbers between
the range
int l,add=0;
//initialising l with first even
number in the range
if(x%2==0){
l=x+2;
}
else{
l=x+1;
}
//performing the summation
for(int i=l;i<y;i=i+2){
add=add+i;
}
cout<<"summation of range is :"<<add<<endl;
}
};
int main() {
// your code goes here
//object created of class evenNumber to perform or
call the functions
evenNumber ob;
//functions are called with different parameters
ob.sum(2,81);
ob.sum(5);
ob.sum();
return 0;
}
OUTPUT:
Success #stdin #stdout 0s 4276KB
summation of range is :1638 summation of range is :2444 summation of range is :2450