Power Function Recursive

Power Function Recursive

Easy C Functions 34 views
Explanation Complexity

Problem Statement

Create a recursive function to calculate x^n. If n is negative, return 1/x^|n|. If n=0, return 1. Track the depth of the recursion.

Input Format

Two values:

• x (number)

• n (integer power, can be positive, zero, or negative)

Output Format

The value of xⁿ and the recursion depth.

Example

x = 2
n = -3


Result = 0.125
Recursion depth = 3

Constraints

• x ≠ 0 when n is negative

• n can be negative, zero, or positive

Concept Explanation

The function calculates power using recursion and handles special cases for n = 0 and n < 0.
Recursion depth increases by 1 on each recursive call.

Step-by-Step Explanation

1.Create a recursive function that takes x, n, and a depth counter.

2.Base case 1:

• If n == 0:

• Return 1.

3.Base case 2 (negative power handling):

• If n < 0:

• Convert problem to 1 / (x ^ |n|).

4.Recursive case (positive n):

• Return x × power(x, n − 1).

5.Increase recursion depth on every function call.

6.Stop recursion when a base case is reached.

7.Final returned value is the computed power, and depth equals the number of recursive calls made.

Concept Explanation

The function calculates power using recursion and handles special cases for n = 0 and n < 0.
Recursion depth increases by 1 on each recursive call.

Step-by-Step Explanation

1.Create a recursive function that takes x, n, and a depth counter.

2.Base case 1:

• If n == 0:

• Return 1.

3.Base case 2 (negative power handling):

• If n < 0:

• Convert problem to 1 / (x ^ |n|).

4.Recursive case (positive n):

• Return x × power(x, n − 1).

5.Increase recursion depth on every function call.

6.Stop recursion when a base case is reached.

7.Final returned value is the computed power, and depth equals the number of recursive calls made.

Input / Output Format

Input Format
Two values:

• x (number)

• n (integer power, can be positive, zero, or negative)
Output Format
The value of xⁿ and the recursion depth.
Constraints
• x ≠ 0 when n is negative

• n can be negative, zero, or positive

Examples

Input:
x = 2 n = -3
Output:
Result = 0.125 Recursion depth = 3

Example Solution (Public)

C
#include <stdio.h>

/* Recursive power function */
double power(double x, int n, int depth) {
    printf("Recursion depth %d: power(%.2f, %d)n", depth, x, n);

    /* Base case */
    if (n == 0)
        return 1;

    /* Negative power */
    if (n < 0)
        return 1 / power(x, -n, depth + 1);

    /* Positive power */
    return x * power(x, n - 1, depth + 1);
}

int main() {
    double x;
    int n;

    printf("Enter value of x: ");
    scanf("%lf", &x);

    printf("Enter value of n: ");
    scanf("%d", &n);

    double result = power(x, n, 1);

    printf("nResult: %.2f^%d = %.5fn", x, n, result);

    return 0;
}

Official Solution Code

#include <stdio.h>

/* Recursive power function */
double power(double x, int n, int depth) {
    printf("Recursion depth %d: power(%.2f, %d)n", depth, x, n);

    /* Base case */
    if (n == 0)
        return 1;

    /* Negative power */
    if (n < 0)
        return 1 / power(x, -n, depth + 1);

    /* Positive power */
    return x * power(x, n - 1, depth + 1);
}

int main() {
    double x;
    int n;

    printf("Enter value of x: ");
    scanf("%lf", &x);

    printf("Enter value of n: ");
    scanf("%d", &n);

    double result = power(x, n, 1);

    printf("nResult: %.2f^%d = %.5fn", x, n, result);

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.