Program in C to Find Power of a Number

Program in C to Find Power of a Number

Finding the power of a number means multiplying a base number by itself a certain number of times, determined by the exponent. This program demonstrates different methods to calculate powers in C, including direct multiplication using loops, using a custom function, and using the pow() function from the math library.

Understanding The Problem

The program should take two inputs from the user: a base and an exponent. It should calculate the result of raising the base to the given exponent. The program should handle positive and zero exponents, and optionally negative exponents using floating-point division.

Steps to Solve the Problem:

  • Take input for the base and exponent from the user.
  • Use a method to calculate the power.
  • Display the result.

Solution 1: Using a Loop

This method multiplies the base repeatedly according to the exponent.

#include <stdio.h>

int main() {

    int base, exponent, result = 1;

    printf("Enter base: ");
    scanf("%d", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    for(int i = 1; i <= exponent; i++) {
        result *= base;
    }

    printf("%d raised to the power %d is %d\n", base, exponent, result);

    return 0;

}

The program multiplies the base by itself exponent times using a loop. This method is straightforward and works for positive integer exponents.

Solution 2: Using a Function

Using a function improves code readability and allows reuse of the power calculation in multiple places.

#include <stdio.h>

int power(int base, int exponent) {

    int result = 1;

    for(int i = 1; i <= exponent; i++) {
        result *= base;
    }

    return result;

}

int main() {

    int base, exponent;

    printf("Enter base: ");
    scanf("%d", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    printf("%d raised to the power %d is %d\n", base, exponent, power(base, exponent));

    return 0;

}

The power() function handles the multiplication, making the main program cleaner. It can be reused whenever power calculation is needed.

Solution 3: Using Recursion

The recursive function power() multiplies the base by the result of calling itself with the exponent decremented by 1.

#include <stdio.h>

// Recursive function to calculate power
double power(double base, int exponent) {

    if(exponent == 0)  // Base case: any number raised to 0 is 1
        return 1;

    else if(exponent > 0)  // Positive exponent
        return base * power(base, exponent - 1);

    else  // Negative exponent
        return 1 / power(base, -exponent);

}

int main() {

    double base;
    int exponent;

    printf("Enter base: ");
    scanf("%lf", &base);

    printf("Enter exponent: ");
    scanf("%d", &exponent);

    printf("%.2lf raised to the power %d is %.5lf\n", base, exponent, power(base, exponent));

    return 0;

}

In this program, the recursion works as follows: if the exponent is zero, the function returns 1. If the exponent is positive, it multiplies the base by power(base, exponent - 1). If the exponent is negative, it returns the reciprocal of the positive exponent result, handling negative powers effectively.

Solution 4: Using the pow() Function

The C standard library provides the pow() function in <math.h> to calculate any power, including fractional or negative exponents.

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

int main() {

    double base, exponent, result;

    printf("Enter base: ");
    scanf("%lf", &base);

    printf("Enter exponent: ");
    scanf("%lf", &exponent);

    result = pow(base, exponent);

    printf("%.2lf raised to the power %.2lf is %.2lf\n", base, exponent, result);

    return 0;

}

Using pow(base, exponent) directly calculates the result and works for integers, decimals, and negative exponents, making it the most flexible method.

FAQs

Q1: Can the exponent be negative?
Yes, using pow() or a floating-point function allows negative exponents, resulting in a fractional number.

Q2: Which method is best?
For integers, loops or functions are simple and efficient. For decimals or negative exponents, the pow() function is most appropriate.

Q3: Can this program handle decimal bases?
Yes, using double types allows the program to calculate powers of decimal numbers accurately.

Conclusion

This program demonstrates multiple ways to calculate the power of a number in C. Loops and functions are suitable for integer exponents, while pow() provides flexibility for decimals and negative powers. The logic can be applied to mathematical and scientific programs requiring exponentiation.

References & Additional Resources

Scroll to Top