Question

In: Computer Science

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

Solutions

Expert Solution

The answer to this question is as follows:

The programming language that I choose is C language

The code is as follows:

#include<stdio.h>
int sum_of_even(int num1,int num2)
{
int sum=0;
for(int i=num1;i<=num2;i++)
{
if(i%2==0)
sum=sum+i;
}
return sum;
}
int sum_of_odd(int num1,int num2)
{
int sum1=0;
for(int i=num1;i<=num2;i++)
{
if(i%2!=0)
sum1=sum1+i;
}
return sum1;
}
int main() {
int num1,num2;
scanf("%d %d",&num1,&num2);
printf("Sum of even numbers between %d and %d is %d\n",num1,num2,sum_of_even(num1,num2));
printf("Sum of odd numbers between %d and %d is %d",num1,num2,sum_of_odd(num1,num2));

}

The input and ouput are provide in the screenshot below:


Related Solutions

Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to...
Write a function called alter_sum(n)that returns the alternating sum of all the numbers from 1 to n so that the first number is added, the second number is subtracted, the third number added, the fourth subtracted, and so on: 1-2+3-4+5-6+7… until you reach n. If n is 0 or less then return 0.
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a...
Write a C function called weighted_digit_sum that takes a single integer as input, and returns a weighted sum of that numbers digits. The last digit of the number (the ones digit) has a weight of 1, so should be added to the sum "as is". The second from last digit (the tens digit) has a weight of 2, and so should be multiplied by 2 then added to the sum. The third from last digit should be multiplied by 1...
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Write a loop that will calculate the sum of all even numbers from 2 to 30...
Write a loop that will calculate the sum of all even numbers from 2 to 30 ( including 30) store the result in the variable called thirtySum. Declare and initialize all variables. Answer using programming in c.
In C++, write a function that takes in as inputs two arrays, foo and bar, and...
In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.
write the “largerComponents” method that takes in two integer arrays and returns true or false if...
write the “largerComponents” method that takes in two integer arrays and returns true or false if the first array’s components are strictly greater than the second array’s components. The arrays must be the same size or else return false. Clarification Note: Components meaning each value at a specific index. For instance, if we had the arrays {5,2,7} and {1,3,1} then this method would return false as the value “2” is not greater than “3”. here is a provided code //Do...
Write a Python program that calls a function to sum all the numbers in a list...
Write a Python program that calls a function to sum all the numbers in a list and returns the result to the caller. The main program creates a list (with hard-coded or user input) and passes the list as an argument to the function. You may not use the built-in function, sum. The program calls a second function to multiply all the numbers in a list passed to it by main and returns the product back to the caller. List...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT