Power Function in C/C++

CC++Server Side ProgrammingProgramming

Power function is used to calculate the power of the given number.

The pow function find the value of a raised to the power b i.e. ab.


double pow(double a , double b)

It accepts a double integers as input and output a double integer as output. It pow() function is defined in math.h package.

If you pass an integer to the power function, the function converts it into double data type. But there's an issue with this, sometimes this conversion might store this as a lower double. For example, if we pass 3 and is converted as 2.99 then the square is 8.99940001 which converts into 8. But this is an error although it comes rarely but to remove this error 0.25 is added to it.

Example Code

#include <stdio.h>
#include <math.h>
int main() {
   double x = 6.1, y = 2;
   double result = pow(x, y);
   printf("%f raised to the power of %f is %f \n" ,x,y, result );
   // Taking integers
   int a = 5 , b = 2;
   int square = pow(a,b);
   printf("%d raised to the power of %d is %d \n", a,b, square );
   return 0;


6.100000 raised to the power of 2.000000 is 37.210000
5 raised to the power of 2 is 25
Updated on 30-Jul-2019 22:30:26