# For loop and exponents

This is a discussion on For loop and exponents within the C++ Programming forums, part of the General Programming Boards category; Hello, I'm caught in a somewhat difficult question in my C++ course at college. The question pertains to for loops ...

1. ## For loop and exponents

Hello,

I'm caught in a somewhat difficult question in my C++ course at college. The question pertains to for loops and the calculation of exponents. The question is as follows:

Start with integer objects n and product. Initialize product = 1 and use the loops to output the powers (n^1, n^2... n^6) for exponents 1 through 6
This specific question deals with the for loop. I'm not sure how to go about performing this calculation. First, I could increment the variable product while product is <= to 6 to get all six exponents recognized. But then, where would the answer for each respective operation be stored. I'm not sure how to calculate exponents either without using a massive set of if-else statements, which may or may not be permitted on this question (I don't believe its permitted)

Thank you for your help,
Travis

2. It is your assignment so I wouldn't want to do it for you but here is some code that you can examine:

Code:
```int main(void) {
int i, array[6];

for(i = 0; i < 6; i++)
array[i] = 1<<i;

return 0;
}```
Before you wet yourself thinking I just did it for you I remind you that this only does what you want for powers of 2. If you are permitted to do so I would make a recursive function. If not I would just put a nested loop inside the main for that would do the calculations. This is one of the first assignments I've heard of that has real world utility.

3. Thank you for your help, but however, we haven't gotten into arrays yet (thats next chapter) so arrays are thrown out lol

anyway to do it without an array?

we have just stepped foot in for loops (this is an introduction to C++ course) and we havent reached recursive functions yet (that is also next chapter)

thank you,
Travis

4. Try to display the following using for loops, and you might start to get an idea of how they work, and how to use them:

*
**
***
****
*****

then do the reverse:

*****
****
***
**
*

See if you can get each of them to display using 'for' loops. That should be a start in the right direction

5. Thank you very much, I figured out how to do the triangle *'s. I'm starting to formulate ideas on how to go about this. A nested for loop makes sense but I'm not quite sure about the implementation... should I use another object as a control object in the nested for loop (besides n?)

Thanks for your help guys, it's really appreciated

Travis

6. Okay guys, check this code out and see if you believe it's okay for the problem required. I believe I got it:

Code:
```#include <iostream.h>

void main()
{
long n, product, exponentSum;
cout << "Define n--";
cin >> n;
exponentSum = n;
cout << n << endl;
for (product=1; product < 6; product++)
{
exponentSum *= n;
cout << exponentSum << endl;

}
}```
Thank you guys again for your help... let me know if you believe this is thumbs up or down

Travis

Popular pages Recent additions