pow() function in C
pow() function is one of the in-built functions present in math.h header file Power function is used to calculate the powers of the given number. The syntax of the power function is pow(a,b).
The function gives the result as a^b. To use this function, we have to include the math.h header file. We have to pass two values as the argument in the function. It considers the first value as the base and the second as the exponent.
We can pass integer, float and double into this function. We can’t pass character values. By default, it takes the double value and returns a double value.
Ex:-
pow(2,3)
It gives the result as 8
Example program
// program for finding powers of the given number using pow() function in static way
#include<stdio.h>
#include<math.h>
Int main()
{
int base= 2;
int exponent= 5;
int c;
c= pow(2,5); // Function calling
// printing the power of 2
printf(“The fifth power of 2 is= %d ”,c);
return 0;
}
Output :
test case 1
The fifth power of 2 is = 32
// program for finding power of the given number using pow() function in Dynamic way
#include<stdio.h>
#include<math.h>
int main()
{
int base, exponent, c;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent);
c=pow(base, exponent);// function calling and passing base and exponent values
// printing the power of base
printf(“\n %d power of %d is =”, exponent,base,c);
return 0;
}
Output :-
test case 1
Enter the base value: 3
Enter the exponent value: 2
2 power of 3 is= 9
//program to find power with out using pow() function in non recursive method
#include<stdio.h>
#include<math.h>
int main()
{
int base, exponent, power, i;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent); // scanning the exponent value
for( i=1; i<=exponent; i++ )
{
Power = power * base ;
}
printf(“ %d power of %d is = %d ”, exponent, base, power );
return 0;
}
Output :-
test case 1
Enter the base value: 10
Enter the exponent value: 2
2 power of 10 is= 100
//program to find power with out using pow() function in recursive method
#include<stdio.h>
#include<math.h>
int calculate ( int a, int b );// function declaration
int calculate (int a, int b ) //function body
{
int i;
if( b = = 0 )
{
return 1;
}
Else
{
Return ( a * calculate (a, b-1));
}
}
int main()
{
int base, exponent, power ;
printf(“ Enter the base value :\n ”);
scanf(“ %d”, &base);\\ scanning base value
printf(“ \nEnter the exponent value :\n “, &exponent);
scanf(“ %d”, &exponent); // scanning the exponent value
power = calculate( base, exponent ); // function calling
printf(“ %d power of %d is = %d ”, exponent, base, power );
return 0;
}
Output :-
test case 1
Enter the base value: 5
Enter the exponent value: 3
3 power of is= 125
Advantages of pow() function:-
1 . Complexity of the code gets reduced
2 . The code will be simple and easy to understand