In: Computer Science
"sum_between" function
Write a function named "sum_between" that receives 2 parameters -
"start" (an int) and "end" (an int). It should return the sum
(total) of all of the integers between (and including) "start" and
"end".
If "end" is less than "start", the function should return -1
instead.
e.g. if you give the function a start of 10 and an end of 15, it
should return 75 (i.e. 10+11+12+13+14+15)
I write this program in c language:
#include<stdio.h>
int sum_between(int start,int end){
int sum=0;
int i=0;
if(end<=start){
return -1;
}
for(i=start;i<=end;i++){
sum=sum+i;
}
return sum;
}
int main()
{
int a,b;
printf("Enter number 1: ");
scanf("%d",&a);
printf("Enter number 2: ");
scanf("%d",&b);
int res = sum_between(a,b);
printf ("Output: %d", res);
return 0;
}