Answer to Question #24390 in C++ for Nour

Question #24390
Write a function called Sum that calculates and returns the sum of all numbers from 1 through the int passed to the function.
1
Expert's answer
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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS