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 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

Let power(a, b) means ab.
  If b is even : power(a, b) = power(a, b/2)*power(a, b/2);
  if b is odd : power(a, b) = power(a, b/2)*power(a, b/2)*a;
For example: 
  28 = 24 * 24;
  29 = 24 * 24 * 2;
Once we calculated power(a, b/2), we don't have to calculate it again. We can simply square it.
Let square(x) function returns the value of x2.
Now, 
  If b is even : power(a, b) = square(power(a, b/2));
  If b is odd : power(a, b) = square(power(a, b/2))*a;
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 (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 (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

Related Topics
C program to find hcf and lcm of two numbers
C Program to find nPr and nCr
C program to reverse a number
C program to add digits of a number
C Program to calculate factorial of a number
C program to convert string to integer
C program to sort characters of a string
List of all C programs