Question

In: Computer Science

Make a function that calculates the summation of even numbers in the range. The function sum...

  • 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.

    • The function uses default parameters for the range. When we call this function with one argument, it will be used as a starting point and the end of the range will be 100. Also, the function is called without any argument, the default range (0,100) will be used.

We will use default parameters 0 and 100 for each parameter respectively.

  • For example,
    sum(20, 50) : sum of even numbers between 20 and 50.
    sum(30) : The second parameter is a default value. The range will be (30, 100)
    sum( ) : Both parameters are default values. The range will be (0, 100)

Solutions

Expert Solution

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

Related Solutions

complete in python The function sumEven should return the sum of only the even numbers contained...
complete in python The function sumEven should return the sum of only the even numbers contained in the list, lst. Example list_of_nums = [1, 5, 4, 8, 5, 3, 2] x = sum_evens(list_of_nums) print(x) #prints 14 Start your code with def evens(lst):
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Input 10 integers and display the following: a. the sum of even numbers. b. the sum...
Input 10 integers and display the following: a. the sum of even numbers. b. the sum of odd numbers. c. the largest integer d. the smallest integer e. the total count of even numbers f. the total count of odd numbers. Using C++ program with for loop..
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If...
Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result.
show that every function can be expressed as the sum of an even function and an...
show that every function can be expressed as the sum of an even function and an odd function and that there is only one way to do this.
What is the function and argument to calculate the sum of the values in the range...
What is the function and argument to calculate the sum of the values in the range D1:D50 for which in the adjacent value in the range A1:A50 equals “Senior” and the adjacent value in the range B1:B50 equals “B”? What is the function and argument to calculate the average of the cells in the range C1:C50 for which the adjacent cell in the range B1:B50 equals “B”? What is the function and argument to return the index number of the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the...
Bash script: Create a bash script that takes numbers as parameters, calculates sum and prints the result. If no parameters passed, prompt a user (only one time) to enter numbers to sum and print the result. Submit your program as LastName_FirstName.sh file.
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the...
Question4: Create and use a lambda expression which calculates the sum of three numbers. Ask the user for three numbers and use the lambda function to print the sum [6 marks] Question5: Write a generator function which generates the Fibonacci series. [0,1,2,4,8,16,…]. The Fibonacci series is a list of numbers where each number is the sum of the previous two. [3 marks] Then write the code to iterate through the first six numbers of sequence. [3 marks] Question6: Write a...
Two numbers are drawn, each in the range {0,1}. What is the probability that their sum...
Two numbers are drawn, each in the range {0,1}. What is the probability that their sum is less than 1 while their product is greater than 1/5?
use Python The assertion that every even number is the sum of two prime numbers is...
use Python The assertion that every even number is the sum of two prime numbers is called Goldbach’s conjecture. You will write a program that asks the user for an integer number, then checks if the integer is even, and finally determines two prime numbers that sum to the user’s entered integer number. Assignment Requirements Three functions are required: get_input(): This function takes no parameters, but will ask the user for an integer number. It will return a valid integer....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT