How to calculate exponents using for loops?

Using a for loop, how would you compute the result of an int raised to the power of another int?

I'm sure the for loop would need two variables, one for the base and one for the exponent. I know it must calculate the base raised to the expoint.
It is something like this :

1
2
3
4
5
int a = 5; // base
int e = 3; // exponent 
int result = 1;

for (int i = 0;i < e;++i)result *= a;


Well, if the exponent is zero (5^0), the result becomes one.
Last edited on
It could be:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int power(int base, int exponent)
{
	if (exponent >= 0)
	{
		int result = 1;
		for (int i = 0; i < exponent; ++i)
		{
			result *= base;
		}
		return result;
	}
	else
	{
		throw std::exception("exponent < 0");
	}
}
Hi,

And if the exponent is negative ?
To handle negative exponent and even base 0:
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
27
28
29
30
double power(int base, int exponent)
{
	if (exponent == 0)
	{
		return 1;
	}
	else if (base == 0)
	{
		return 0;
	}
	else if (exponent > 0)
	{
		int result = base;
		for (int i = 1; i < exponent; ++i)
		{
			result *= base;
		}
		return result;
	}
	else
	{
		double base_d = static_cast<double>(base);
		double result = 1.0 / base_d;
		for (int i = -1; i > exponent; --i)
		{
			result /= base_d;
		}
		return result;
	}
}


But, I'm not so expert in mathematics, so maybe it is a little bit complicated.
I think the fomula for negative exponent case is "1 / ( base ^ positive exponent )". I don't want to add a second for-loop, so this probably might be the best :

1
2
3
4
5
6
7
8
9
int a = 5; // base
int e = -3; // exponent 

int p_e = abs(e); // exponent (positive) used with for-loop
double result = 1;

for (int i = 0;i < p_e;++i)result *= a;

if(e < 0)result = 1.0 / result;

@fcantoro

11
12
13
14
15
16
17
18
19
	else if (exponent > 0)
	{
		int result = base;    // make this a double to fix problem on next line
		for (int i = 1; i < exponent; ++i)  // result overflows if exponent > 32
		{
			result *= base;
		}
		return result;
	}


With the negative exponent, I found it easier to do multiplication just as if the exponent was positive (use that code), then find the reciprocal at the end. There there is one division operation , and no casting.

The parameters to the function should both be const.

It would be a good idea to handle 00 and 0x where x < 0, which are undefined.

Hello, thank you for all your responses. I got it to compile and run, but the result won't output on the console.

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
27
28
29
30
31
32
33
34
35
#include <iostream>
using namespace std;

int pow(int base, unsigned int exponent)
{
	if (exponent >= 0)
	{
		int result = 1;
		for (int i = 0; i < exponent; ++i)
		{
			result *= base;
		}
		return result;
	}
	else if ( exponent == 0)
	{
		return 1;
	}
}

int main()
{
	int x, y;

	cout << "Enter base value: " << endl;
	cin >> x;

	cout << "Enter exponent value: " << endl;
	cin >> y;

	pow(x, y);

    system("PAUSE");
	return 0;
}


Instead of return result (on line 13) I even tried cout << result;

Why won't the result output?
I got it to compile and run, but the result won't output on the console.


Line 31, you call the function but don't do anything with it - it needs to be assigned to a variable or the function call should be in the std::cout.

Line 31: std::pow is the library function. You have got away with here, only because you don't include <cmath> this is a very good reason not to have line 2.
Hi,

Your code will overflow an int if exponent is greater than 32. You should check for this.

Lines 15 - 18 are unreachable because of the overlap with the test on line 6.

 In function 'int pow(int, unsigned int)': 
6:15: warning: comparison of unsigned expression >= 0 is always true [-Wtype-limits] 
9:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 


Always set the warning level for the compiler to it's highest.
Last edited on
@Wulfinite

Why don't you try mine first?

It is very simple enough to begin with and all you need to do is insert cout << result; at the end. Modifying "a" or "e" will vary the result.

I'm certain that my example shouldn't contain either any possible warning or redundant code. To fully understand the logic behind it, you should test the simpler version first before you can try the more complex and complicated ones.

Of course, my example contains function abs() so you probably need to include <math.h> or <cmath.h>, just in case.
Last edited on
Thank you for all your help everyone!
Topic archived. No new replies allowed.