Good Afternoon,

I'm having some problems with a problem that deals with raising the power of an integer. So far I have this code.

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int num, bigNum, power, count;
cout << "Enter an integer: ";
cin>>num;
cout << "What power do you want it raised to? ";
cin>>power;
bigNum = num;
while(count++ < power)
bigNum *= num;
cout << "The result is " <<bigNum<<endl;
return 0;
}

Dani AI

Generated

’s attempt shows the basic idea; correctly flagged initialization and loop style. Beyond those fixes, four practical concerns matter for a robust integer-power routine: negative exponents (fractional results), the 0^0 ambiguity, integer overflow, and performance for large exponents. Explicit handling of each case makes behavior predictable.

Negative exponents should either be rejected by an integer-only routine or handled by converting to a floating result (for example via std::pow) and documenting the conversion. A non-zero base to the zero power is conventionally 1; 0^0 is indeterminate and deserves an explicit policy (return 1, return 0, or signal an error). Prefer an unsigned type for the exponent when only non-negative powers are intended.

A fast, common integer-only algorithm is exponentiation by squaring. It computes the power in O(log n) multiplications (no overflow checks shown here):

long long int_pow(long long base, unsigned long long exp) {
    long long result = 1;
    while (exp) {
        if (exp & 1) result *= base;
        base *= base;
        exp >>= 1;
    }
    return result;
}

Overflow must be considered: either detect it (compare against LLONG_MAX / abs(x) before multiplying, or use a wider intermediate type such as __int128 to test results) or switch to an arbitrary-precision type (for example boost::multiprecision::cpp_int) when very large results are expected. For fractional or negative-exponent needs, std::pow is convenient but returns a floating type and is subject to rounding; document that behavior. Combining ’s initialization/style advice with explicit edge-case checks, a fast algorithm, and overflow handling yields a reliable implementation.

The two problems I see (besides the lack of indentation ;) ) is that you don't initialize the value of count and you have one too many iterations. A more correct code would be this:

bigNum = 1;
count = 0;
while(count++ < power)
  bigNum *= num;

Or, even better, if you try to stick with more idiomatic (or "usual") code, you should write:

int bigNum = 1;  // initialize, at the site of first use (initialization).
for(int count = 0; count < power; ++count)  // use a for-loop when you count iterations.
  bigNum *= num;

Sticking to conventional coding style is not just for esthetics, it also helps you and others to understand what you are doing, and bugs are easier to find too (because you easily see missing pieces, and formulating idiomatic code becomes a second nature).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.