Calculate Power of a Number Using Pow() Function

0

In this example two numbers will be taken from the user as an input. One of the numbers act like a base number and the other number as an exponent.

#include <stdio.h>
#include <math.h>

int main() 
{
    double base, result;
    int exponent;
    
    printf("Please enter the base number: ");
    scanf("%lf", &base);
    
    printf("Please enter the exponent number: ");
    scanf("%d", &exponent);
    
    result = pow(base, exponent);
    
    printf("Result: %.2lf", result);
    return 0;    
}

To calculate power of number we need a pow() function which resides in the math.h library. For this purpose, we need to include math.h library in the beginning. After this, three new variables are declared. Two variables will be of type double namely base and result. The base variable stores the base number entered by the user and the result variable stores the end result in the variable. One variable is of type integer namely exponent which holds the exponent value entered by the user.

In line 15, the pow() function is called. The syntax of pow() function is,

pow(base, exponent)

First, the base number will be passed and then the exponent number will be passed. The calculation will look like this,

Base number raised to the power of the exponent number.

Finally, the result after using the pow() funtion is displayed on the console. We only want to show two decimal numbers after decimal point that’s why we will use .2lf which only displays two numbers after decimal point.

Output

Please enter the base number: 6.7
Please enter the exponent number: 4
Result: 2015.11

LEAVE A REPLY

Please enter your comment!
Please enter your name here