Write a C program to input any two numbers x and y and find their power using inbuilt pow() function. How to find power of two numbers in C programming. How to use pow() function in C programming. C program to compute power of two numbers using predefined library function.
Example
Input
Enter base: 5 Enter exponent: 2
Output
x ^ y = 25
Required knowledge
Fundamentals of C, Data types, Talking user input in C
Program to find power of a number
/**
* C program to find power of any number
*/
#include <stdio.h>
#include <math.h> //Used for pow() function
int main()
{
double x, y, power;
// Reads two numbers from user to calculate power
printf("Enter base: ");
scanf("%lf", &x);
printf("Enter exponent: ");
scanf("%lf", &y);
// Calculates x^y
power = pow(x, y);
printf("x ^ y = %.2lf", power);
return 0;
}
Before you move on to next program or exercise learn how to find power using other programming methods -
Output
Enter base: 5 Enter exponent: 3 x ^ y = 125
Note: %.2lf is used to print fractional value upto 2 decimal places.
Happy coding ;)
Recommended posts
- Basic programming exercises index.
- C program to find square root of any number.
- C program to convert temperature from Celsius to Fahrenheit.
- C program to convert days to years, weeks and days.
- C program to calculate total, average and percentage.
- C program to calculate Simple Interest.
- C program to enter month number and print total number of days in month.