Sunday, July 30, 2017

Write a function named "sum_from_to" that takes two integer arguments, call them "first" and "last", and returns as its value the sum of all the integers between first and last inclusive.

/*
For more exercises and lessons, visit http://shegertech.blogspot.com/
*/

#include <iostream>
#include <cstdlib>
using namespace std;



int sum_from_to (int first, int last)
{
int i, partial_sum = 0;
if (first <= last)
for (i = first; i <= last; ++i)
partial_sum += i;
else
for (i = first; i >= last; --i)
partial_sum += i;
return partial_sum;
}

int main(){

int n1=2, n2=5;
cout<<sum_from_to(n1, n2)<<endl;
system("pause");
return EXIT_SUCCESS;
}

Share: