Given two numbers base and exponent, pow() function finds x raised to the power of y i.e. xy. Basically in C exponent value is calculated using the pow() function.
Example:
Input: 2.0, 5.0 Output: 32 Explanation: pow(2.0, 5.0) executes 2.0 raised to the power 5.0, which equals 32 Input: 5.0, 2.0 Output: 25 Explanation: pow(5.0, 2.0) executes 5.0 raised to the power 2.0, which equals 25
Syntax:
double pow(double x, double y);
Parameters: The method takes two arguments:
- x : floating point base value
- y : floating point power value
Program:
C
// C program to illustrate // power function #include <math.h> #include <stdio.h> int main() { double x = 6.1, y = 4.8; // Storing the answer in result. double result = pow(x, y); printf("%.2lf", result); return 0; } |
C++
// CPP program to illustrate // power function #include <bits/stdc++.h> using namespace std; int main() { double x = 6.1, y = 4.8; // Storing the answer in result. double result = pow(x, y); // printing the result upto 2 // decimal place cout << fixed << setprecision(2) << result << endl; return 0; } |
5882.79
Working of pow() function with integers
The pow() function takes ‘double’ as the arguments and returns a ‘double’ value. This function does not always work for integers. One such example is pow(5, 2). When assigned to an integer, it outputs 24 on some compilers and works fine for some other compilers. But pow(5, 2) without any assignment to an integer outputs 25.
- This is because 52 (i.e. 25) might be stored as 24.9999999 or 25.0000000001 because the return type is double. When assigned to int, 25.0000000001 becomes 25 but 24.9999999 will give output 24.
- To overcome this and output the accurate answer in integer format, we can add 0.5 to the result and typecast it to int e.g (int)(pow(5, 2)+0.5) will give the correct answer(25, in above example), irrespective of the compiler.
C
// C program to illustrate // working with integers in // power function #include <math.h> #include <stdio.h> int main() { int a; // Using typecasting for // integer result a = (int)(pow(5, 2) + 0.5); printf("%d", a); return 0; } |
C++
// CPP program to illustrate // working with integers in // power function #include <bits/stdc++.h> using namespace std; int main() { int a; // Using typecasting for // integer result a = (int)(pow(5, 2) + 0.5); cout << a; return 0; } |
25
This article is contributed by Arushi Dhamija and Jatin Goyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- What happens when a virtual function is called inside a non-virtual function in C++
- Function Overloading vs Function Overriding in C++
- Difference between Virtual function and Pure virtual function in C++
- How to call function within function in C or C++
- Difference between virtual function and inline function in C++
- Write a one line C function to round floating point numbers
- Does C support function overloading?
- How can I return multiple values from a function?
- What is the purpose of a function prototype?
- How to declare a pointer to a function?
- C function to Swap strings
- Can we call an undeclared function in C++?
- What is evaluation order of function parameters in C?
- Can we use function on left side of an expression in C and C++?
- Declare a C/C++ function returning pointer to array of integer pointers
- Function overloading and return type
- Function Pointer in C
- Importance of function prototype in C
- What happens when a function is called before its declaration in C?
- Default arguments and virtual function

