for loop

hi
i start to learn c++. there is a lab asking
write a program to add between 1 to 20 using a for loop. Check that the program
ontained the right answer by using the formula for sum of the number 1 to n:
n(n+1)/2
i know its very easy but if you know!!!
can any body help me????
Psuedocode:

1
2
3
4
5
6
7
8
9
for(int i = 1; i <= 20; ++i)
{
  // add i to a sum variable
}

// output sum from loop arithmetic

// calculate sum using formula
// output formula answer 
You can use an accumulator in a for loop like this

# include <iostream.h>
void main()
{
clrscr();
int ctr, sum=0; // Defining counter and accumulator
for (ctr=1;ctr<=20;ctr++)
{ sum++;
}
Does this help??
}

Topic archived. No new replies allowed.