C Program To Find The power Of A Number Using Recursion

Share Your Love

In this post I am going to explain you how to find the power of a number using recursion.

Here we input base and power of number and find the power of a number using recursion by help of C programming.

Example:

Input

Input base number: 5
Input power: 2

Output

2 ^ 5 = 25

Required knowledge:

  1. Basic C Programming, if else, Functions and recursion.
  2. Also know power of a number using pow() and without pow() function.

Declare Recursively Function To Find Power:

Recursive function to calculate power

The above mathematical formula to find power of number.

The function accepts two numbers i.e. x and y and calculates x ^ y.

Now the above mathematical function to transform C programming context.

  1. First, give a meaningful name to our recursive function say pow().
  2. The function accepts two numbers i.e. base and exponent to calculate its power.
  3. Then takes two parameters base and exponent say, pow(double base, int exponent);
  4. Finally function return base ^ exponent i.e. double type value.

The finally declares to calculate power is double double pow(double base, int exponent);

Logic to calculate the power of a number using recursion:

So, now its time to use of logic to find out power recursively. There can be three cases while calculating power of a number.

Step-1:

If exponent is 0, then power comes to 1. This is the base condition of our recursive function. 

Step-2:

If exponent is negative, then power is 1 / (x ^ -y). Which uses recursive call to pow() function for computing the value of (x ^ -1) i.e. 1 / pow(base, -expo).

Step-3:

If exponent is positive, then we calculate the power normally using x ^ y. Computing x ^ y call recursive call i.e. base * pow(base, expo - 1)

Now combine all three conditions in a single recursive function:

/**
 * C program to find power of a number using recursion
 */

#include <stdio.h>


/* Power function declaration */
double pow(double base, int expo);


int main()
{
    double base, power;
    int expo;
    
    /* Input base and exponent from user */
    printf("Enter base: ");
    scanf("%lf", &base);
    printf("Enter exponent: ");
    scanf("%d", &expo);
    
    // Call pow function
    power = pow(base, expo); 
    
  	//print base, expo and power upto 2 fractional places
    printf("%.2lf ^ %d = %f", base, expo, power);
    
    return 0;
}


/**
 * Calculate power of any number.
 * Returns base ^ expo
 */
double pow(double base, int expo)
{
    /* Base condition */
    if(expo == 0)
        return 1;
    else if(expo > 0)
        return base * pow(base, expo - 1);
    else
        return 1 / pow(base, -expo);
}

Output:

Enter base: 2
Enter exponent: 5
2.00 ^ 5 = 32.000000

Please comment and share this post if you find helpful. And wants to join the community then WhatsApp us.

Share Your Love
Avatar photo
Lingaraj Senapati

Hey There! I am Lingaraj Senapati, the Founder of lingarajtechhub.com My skills are Freelance, Web Developer & Designer, Corporate Trainer, Digital Marketer & Youtuber.

Articles: 429

Newsletter Updates

Enter your email address below to subscribe to our newsletter