int answer;
^^^
answer = pow(number1,number2);
pow() function will first convert number1
and number2
to float
. Note that floating point calculations are not exact, which means you will probably get a value 24.999999
instead of exact 25
. After computation, it then casts the result to int
, which will cut off the floating part (e.g. 24.999999 => 24
).
Try to use float
to catch the result:
float answer;
answer = pow(number1,number2);
If you do want to store the result to int
, you should round it correctly:
int answer;
answer = (int) (pow(number1,number2) + 0.5);
To read on, check out What Every Programmer Should Know About Floating-Point Arithmetic.