# Adding output of a FOR loop

• 06-22-2008
cjohnson412
Adding output of a FOR loop
Hi, I have been trying to figure out how to add the output of a for loop together.

In the part of the program I am working on I am suppose to create a program that adds the odd numbers together and gives me a total output (ex 1,3,5,9,11 = 36) . I am later suppose to do other stuff with those numbers but am pretty confident on that portion of the program.

so far i Have, I have tried things such as adding in a second for loop to add the output together but when i so it seems to change the output of the first for loop, All i need is the total if H, that is all, not it it showing each loop :( Any ideas... Thanks

Code:

``` #include <iostream> #include <iomanip> using namespace std; int main() { int input = 0,         i = 0,         h = 0,         k = 0; cout << "enter a number "; cin >> input;                 for ( i = 1; i <= input * 2; i += 2)                 cout << " i is " << i;         for (d = 0 ; i <= input * 2; i+=2)           {           cout << "h is " << h         return 0; }```
• 06-22-2008
tabstop
So you would have to add to your total inside the for-loop. IOW, if i are your numbers, you would add i to your total variable inside your for loop on i where the print statement is.
• 06-22-2008
cjohnson412
Sorry, I just edited my question, I hope it makes more sense. Basiclly I need to add obtain a total for the amount of odd numbers that a user inputs, Ex, a user enters the number 6, then the program loops to create the odd numbers. I need for the final output to display the total for the 6 odd numbers which would be (1+3+5+7+9+11 = 36)
• 06-22-2008
tabstop
Quote:

Originally Posted by cjohnson412
Sorry, I just edited my question, I hope it makes more sense. Basiclly I need to add obtain a total for the amount of odd numbers that a user inputs, Ex, a user enters the number 6, then the program loops to create the odd numbers. I need for the final output to display the total for the 6 odd numbers which would be (1+3+5+7+9+11 = 36)

So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.
• 06-22-2008
cjohnson412
Quote:

Originally Posted by tabstop
So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.

I'm not sure what you mean, So i would add I where inside the loop, Do you mean add it in the second loop, How do i Show the total variable in the loop first loop (36)... because the output of the the final loop i would be 11, not 36
• 06-22-2008
tabstop
I don't think you quite grasp the concept of loops. You do things INSIDE the loop. After the loop is nothing. That is, who cares what i is when you get done with the loop? Once you get done with the loop, it's too late to do any work. You have to do things INSIDE the loop.

You're worrying me. An example to add up squares:
Code:

```int i, total=0, square; for (i = 0; i < 10; i++) {     square = i*i; /* make the square */     total = total + square; /* add the square in to the total */ }```
• 06-22-2008
cjohnson412
Quote:

Originally Posted by tabstop
So you would have to add to your total inside the for-loop. IOW, you would add i to your total variable inside your for loop on i where the print statement is.

cool thanks, well, I understand what ur saying, so I'll go off of that for now thanks!

Wait, do you think that using a for loop is the best idea to figure this out?
Thanks
cj
• 06-22-2008
tabstop
Quote:

Originally Posted by cjohnson412
Wait, do you think that using a for loop is the best idea to figure this out?
Thanks
cj

A for-loop is ideally suited for doing something a fixed number of times (where by "fixed" I don't necessarily mean that you know as you are writing the program, but it will be known by the time the program reaches your loop).
• 06-22-2008
cjohnson412
Quote:

Originally Posted by tabstop
A for-loop is ideally suited for doing something a fixed number of times (where by "fixed" I don't necessarily mean that you know as you are writing the program, but it will be known by the time the program reaches your loop).

So how did u figure out that this was a square?!?!?!?!?!? Is this a common problem u see people have for classes. I just realized that the numbers were square and increase by one each time, apparently i'm no genus, Just a CIS major! lolz.

THANKS
• 06-22-2008
tabstop
Quote:

Originally Posted by cjohnson412
So how did u figure out that this was a square?!?!?!?!?!? Is this a common problem u see people have for classes. I just realized that the numbers were square and increase by one each time, apparently i'm no genus, Just a CIS major! lolz.

THANKS

Well, it's true that I did know that the sum of odd numbers is a square -- I do have an eighth grade education, after all. That wasn't the point of the example, though -- I'm adding squares to get a different number out in my example (I wanted something similar but not the same).