C program to find power of a number using pow function

Write a C program to input any two numbers x and y from user and find power of both number i.e. x^y using inbuilt library 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.

Also view this program without using other programming methods - C program to find power of any number using loop.
C program to find power of two numbers using recursion.

Example:
Input number1: 5
Input number2: 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 any two number x and y : ");
    scanf("%lf%lf", &x, &y);

    // Calculates x^y 
    power = pow(x, y);

    printf("x^y = %.0lf", power);

    return 0;
} 


Output
Enter any two number x and y : 5
3
x^y = 125

Note: %.0lf is used to print fractional values up to 0 decimal places. You can also use %lf to print fractional values normally up to six decimal places.

Happy coding ;)


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