C program to find power of a number using pow function

Previous Program Next Program

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

Previous Program Next Program
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment