66 935
Assignments Done
99,1%
Successfully Done
In October 2018

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.
Expert's answer
# 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 first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions