C Program to Calculate Power of a Number

  • Write a c program to find power of a number (an)
  • How to find power of a number using recursion.

We have to take two numbers as input from user(base and exponent) and return baseexponent. To calculate power of a number we repetitively multiply number(base) exponent times.

For Example

AN = A*A*A*A..... N times
A5 = A*A*A*A*A

Below programs will not produce correct result if the value of baseexponent exceeds the range of int data type.

C program to calculate power of number using loop

This program takes base and exponent as input from user using scanf function. To calculate baseexponent, it repetitively multiply base with result inside for loop. Finally, it prints the value of result on screen.

Time complexity of this algorithm is O(n).

C Program to Calculate Power of a Number

/*
* C Program to find power of a number (a^b) using loop
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    int base, exponent, counter, result = 1;
    printf("Enter base and exponent \n");
    scanf("%d %d", &base, &exponent);
     
    /* Calculate base^exponent by repetitively multiplying base */
    for(counter = 0; counter < exponent; counter++){
        result = result * base;
    }
     
    printf("%d^%d = %d", base, exponent, result);
    getch();
    return 0;
}

Program Output

Enter base and exponent
2 8
2^8 = 256

C program to find power of a number using divide and conquer

C program to find power of a number using divide and conquer 1

Time complexity of this algorithm is O(logn).
This uses a user defined function power, that implement above mentioned recursive algorithm to find the power of a number. This program can only be used to calculate the power of integers, we cannot use it to calculate power of floating point numbers. If we want to calculate power of floating point number the we should use pow function of math.h header file.

C program to find power of a number using divide and conquer

/*
* C Program to find power of a number (a^b) using divide and conquer
*/
#include <stdio.h>
#include <conio.h>
 
int main(){
    int base, exponent, counter, result = 1;
    printf("Enter base and exponent \n");
    scanf("%d %d", &base, &exponent);
     
    result = power(base, exponent);
     
    printf("%d^%d = %d", base, exponent, result);
    getch();
    return 0;
}
 
int getSquare(int num){
    return num*num;
}
/*
 * Function to calculate base^exponent using recursion
 */
int power(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    if(exponent == 0){
        return 1;
    }
    if(exponent%2 == 1){
        /* If exponent is Odd number 
         * a^b = a*(a^(b/2))*(a^(b/2)) 
         */
        return base * getSquare(power(base, exponent/2));
    } else {
        /* If exponent is Even number
         *  a^b = (a^(b/2))*(a^(b/2))
         */
        return getSquare(power(base, exponent/2));
    }
}

Program Output

Enter base and exponent
2 6
2^6 = 64
Enter base and exponent
2 0
2^0 = 1

C program to find power of a number using recursion

We can use recursion to calculate power of a number because it follows recursive sub-problem structure. This approach reduces the problem of finding an to problem of finding an-1 till exponent becomes 0.
Time complexity of this program is O(n).

power(a, b) = a * power(a, b-1);

C program to find power of a number using recursion

/*
* C Program to find power of a number (a^b) using recursion
*/
#include <stdio.h>
#include <conio.h>
  
int main(){
    int base, exponent, counter, result = 1;
    printf("Enter base and exponent \n");
    scanf("%d %d", &base, &exponent);
    
    result = getPower(base, exponent);
    
    printf("%d^%d = %d", base, exponent, result);
    getch();
    return 0;
}
/*
 * Function to calculate base^exponent using recursion
 */
int getPower(int base, int exponent){
    /* Recursion termination condition,
     * Anything^0 = 1
     */
    if(exponent == 0){
        return 1;
    }
    return base * getPower(base, exponent - 1);
}

Program Output

Enter base and exponent
2 8
2^8 = 256

Leave a Comment