calculate the summation of consecutive positive odd integers from 1 to n

Ok, so i am being a bit of an overachiever tonight, but I'm bored so what!

I am doing my lab for next weeks classes tonight just for some fun but one exercise in the lab really has me puzzled. Maybes its just cause its late and im tired or maybe i am just making it a lot more difficult than it needs to be. BUT anyway.

Here is the assignment:

In this exercise, you need to create a program to calculate the summation of consecutive positive odd integers from 1 to n..

Example: if n=5, 1+3+5+7+9=25. Name the program SumOfOdd.cpp. Make use of the for loop to handle the calculation. Print the result to the screen.

Now i started off with saying that the user is going to input the "n" value. I HAVE to use a "for loop" to show my understanding of for loops for this lab exercise.

This is one attempt that i had:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>
using namespace std;

int main()
{

int n;
int sum = 0;
int number;

cout << "Enter the value for n: ";
cin >> n;

for (number=1; number<=(2*n); number+2)
{
	sum = sum + number;
}
cout << "The sum of ";
cout << n;
cout << " is ";
cout << sum << endl;

return 0;

}


This assignment does not seem very hard but with this code my result is nothing but an input. I can only type an input and nothing happens. Maybe there is something wrong with my syntax for my for loop or i am just not really getting the concept or something. any ideas?

Thanks,
LiverpoolFTW
in the for loop:
 
for(number = 1; number <= 2*n; number+=2)


that should fix it, I think.
--
James
Excellent! Thanks so much. stupid syntax.
No problem, glad to help
--
James
Well, the sum of the first n positive odd integers is exactly equal to n^2.
Topic archived. No new replies allowed.