C Program Power By Recursion

By | March 9, 2020

C Program Power By Recursion

Output

Please Enter base: 2
Please Enter exponent: 4
2^4 = 16
——————————–
Process exited after 32.63 seconds with return value 0
Press any key to continue . . .

/***************************************************
 * C Program to Find N Power P using recursion 
 * Sample Run of Recursive power function 
 * Enter base: 2
 * Enter exponent: 3
 * 2^3 = 8
 ***************************************************/
 
#include<stdio.h>

/* prototype for user defined power() funtion*/
int power(int, int);
 
/* main function */
int main(void)
{    
    int base, exponent;
    
    printf("Please Enter base: ");
    scanf("%d", &base);
    
    printf("Please Enter exponent: ");
    scanf("%d", &exponent);
    
    printf("%d^%d = %d", base, exponent, power(base, exponent));
    
    return 0; 
}
 
 /* Function definition for recursive power() function*/
int power(int base, int exponent)
{    
    
    /*Base condition, Base case
     if power/exponent of any number is zero then result is 1*/
    if(exponent == 0)
    {
        return 1;
    }
    
    else
    {
        /* recursive call*/
        return base * power(base, exponent - 1);
    }
    
}

Power. Base and Exponent

C Program Power by Recursion

C Program Power by Recursion

power is the product of multiplying a number by itself.Normally, a power is represented with a base number and an exponent. The base number represents the number that is being multiplied. The exponent, is a small number written above and to the right of the base number. The exponent tells how many times the base number is being multiplied.

For example, “2 to 3rd power” may be written as “23.” Here, the base number is 2 and the exponent is 3. This means that 2 is being multiplied by itself 3 times that is: 2  x  2  x  2

23     = 2 x 2 x 2 = 8

Loading

Leave a Reply

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