C program to find power of a number using pow function

Write a C program to input two numbers from user and find their power using pow() function. How to find power of a number in C programming. How to use pow() function in C programming.

Example
Input

Enter base: 5
Enter exponent: 2

Output

5 ^ 2 = 25

Required knowledge

Arithmetic operators, Data types, Basic input/output

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 base, expo, power;

    /* Input two numbers from user */
    printf("Enter base: ");
    scanf("%lf", &base);
    printf("Enter exponent: ");
    scanf("%lf", &expo);

    /* Calculates base^expo */
    power = pow(base, expo);

    printf("%.2lf ^ %.2lf = %.2lf", base, expo, power);

    return 0;
}

%.2lf is used to print fractional value up to 2 decimal places.

Also check other approaches to find power of a number.

Read more –

Output

Enter base: 5
Enter exponent: 3
5 ^ 3 = 125

Happy coding 😉