C++/For Loop

Advertisement


Question
Hi
Using C, I was asked to use For Loop to calculate the answer for the sum of 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10.  Although I know how to display these numbers in sequence using a For Loop, but I could not use For Loop to calculate the sum of these numbers.  Any suggestions?  

Answer
Hello,

The following code snippet should help you out and get you pointing in the right direction:

// in main
int main()
{
int iSum = 0, i;

for(i = 0; i <= 10; i++)
iSum += i;

return 0;
}

It sums up all the numbers from 1 to 10 in the for loop.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.