C Program to Power Function Recursive with Explanation
C
Easy
Functions
35 views
2 min read
239 words
This problem helps you practice core C fundamentals in a practical way. It builds intuition around negative, depth, recursion. Let’s break it down step by step so you can implement it confidently.
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.
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 Logic
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.
Code Solution
This explanation is written for learning purposes and to help beginners understand the concept clearly.
#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;
}
Output Example
Output:
Result = 0.125
Recursion depth = 3
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Solution Guide
Problem
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 / Output
Input
Two values:
• x (number)
• n (integer power, can be positive, zero, or negative)
Output
The value of xⁿ and the recursion depth.
Constraints
• x ≠ 0 when n is negative
• n can be negative, zero, or positive
Examples
Output:
Result = 0.125
Recursion depth = 3
Explanation
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.
Details
Common Mistakes
- Misreading input/output format.
- Not handling constraints and edge cases.
- Off-by-one errors in loops.
- Forgetting to reset variables between test cases (if any).
Official Solution
#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;
}
Solutions (0)
No solutions submitted yet. Be the first!