How would I use a for loop in this situation?

How would I use a for loop when I need to write code that will compute the result of an int raised to the power of another int? Does that mean I'd have to make a separate base variable and an exponent variable initialized within the for loop?

For example, using a for loop I'll have to output the result of an int raised to the power of another int. So base variable x = 2, exponent variable = 2, so the output would be for. How would I do this using a for loop?

Before your for loop, ask the user for 2 numbers:
http://www.cplusplus.com/doc/tutorial/basic_io/

Then inside your for loop, use the exponent value to limit the amount of times it loops whilst multiply the variable by itself to calculate the result.
Would you mind elaborating with code?
I think mutexe has answered that quite clear. But if you really need some sample code, I could provide one.

1
2
3
4
5
6
7
8
// ask for 2 numbers
int x, y;
std::cin >> x >> y;

// loop and multiply
int result = 1;
for (int i = 0; i < y; i++)
    result *= x;
Topic archived. No new replies allowed.