c
  1. c-program-to-calculate-the-power-of-a-number

Program to Calculate the Power of a Number - ( C Programs )

Example

Suppose we want to calculate the power of a number 3 raised to 4. The code for calculating the power of a number using C programming language is shown below:

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

int main() {
    double base, exponent, result;
    printf("Enter base number: ");
    scanf("%lf", &base);
    printf("Enter exponent number: ");
    scanf("%lf", &exponent);
    result = pow(base, exponent);
    printf("%.2lf^%.2lf = %.2lf", base, exponent, result);
    return 0;
}

Output

The output for the above example will be:

Enter base number: 3
Enter exponent number: 4
3.00^4.00 = 81.00

Explanation

The above code uses the pow() function from the math.h library to calculate the power of a number. The pow() function takes two arguments - the base number and the exponent number - and returns the result of raising the base number to the exponent number.

The code prompts the user to enter the base number and the exponent number using the scanf() function. The scanf() function reads input from the standard input stream (usually the keyboard) and stores the result in the specified variable.

After reading the input values, the code calls the pow() function with the base and exponent values as arguments. The result is stored in the result variable.

Finally, the code uses the printf() function to display the result in the standard output stream (usually the console).

Use

The power function is widely used in various engineering, scientific, and mathematical applications. It can be used to calculate the work done, energy transfer, and others.

Summary

In summary, the power function is one of the most basic mathematical functions used for calculating the power of a number. The pow() function in C programming language can be used to calculate the power of a number. The code above demonstrates how to use the pow() function to calculate the power of a number in C programming.

Published on: