Answer to Question #24390 in C++ for Nour
Write a function called Sum that calculates and returns the sum of all numbers from 1 through the int passed to the function.
1
2013-02-18T08:31:12-0500
# include <iostream>
using namespace std;
int sum(int a)
{
int res=0;
if (a<1) return 0;
for (int i=1;i<=a;i++)
res+=i;
return res;
}
int main(){
int a=5;
cout<<sum(a);
system("PAUSE");
return 0;
}
Need a fast expert's response?
Submit order
and get a quick answer at the best price
for any assignment or question with DETAILED EXPLANATIONS!
Learn more about our help with Assignments:
C++
Comments
Leave a comment