In: Computer Science
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
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: