As already it was mentioned in comments to your question an object of the type int
can be not large enough to be able to store such values. So substitute the type int
for the type long long
.
For example
#include <stdio.h>
long long int getPower( int base, unsigned int x )
{
long long int result = 1;
while ( x-- ) result *= base;
return result;
}
int main( void )
{
unsigned int x = 5;
int base = 97;
printf( "%d in the power of %u is %lld\n", base, x, getPower( base, x ) );
}
The program output is
97 in the power of 5 is 8587340257
Instead of this statement
printf( "%d in the power of %u is %lld\n", base, x, getPower( base, x ) );
you can write
long long int result = getPower( base, x );
printf( "%d in the power of %u is %lld\n", base, x, result );
An alternative approach is to use for example the float type long double
instead of the integer type long long
as the type of the calculated value.