Power of a Number Using the while Loop

#include <stdio.h>
int main() {
    int base, exp;
    long double result = 1.0;
    printf("Enter a base number: ");
    scanf("%d", &base);
    printf("Enter an exponent: ");
    scanf("%d", &exp);

    while (exp != 0) {
        result *= base;
        --exp;
    }
    printf("Answer = %.0Lf", result);
    return 0;
}

Output

Enter a base number: 3
Enter an exponent: 4
Answer = 81

 

We can also use the pow() function to calculate the power of a number.


Power Using pow() Function

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

int main() {
    double base, exp, result;
    printf("Enter a base number: ");
    scanf("%lf", &base);
    printf("Enter an exponent: ");
    scanf("%lf", &exp);

    // calculates the power
    result = pow(base, exp);

    printf("%.1lf^%.1lf = %.2lf", base, exp, result);
    return 0;
}

Output

Enter a base number: 2.3
Enter an exponent: 4.5
2.3^4.5 = 42.44

The programs above can only calculate the power of the base number if the exponent is positive. For negative exponents, use the following mathematical logic:

base(-exponent) = 1 / (baseexponent)

For example,

2-3 = 1 / (23)

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *