PPS Lab · Experiment 24
x to the power n using recursive and non-recursive functions
CArrays, pointers and functionsJNTUH R22 · CS108ES
Aim
To write a C program that finds xⁿ for a given number x and a non-negative integer n in two ways: with a recursive function and with a non-recursive function, without using the library function pow.
Algorithm
- Read x and n. If n is negative, print a message and stop.
- Recursive function
power_recursive(x, n): if n is 0, return 1. Otherwise return x ×power_recursive(x, n − 1). - Non-recursive function
power_iterative(x, n): set result = 1. Repeat n times: result = result × x. Return result. - Call both functions and print both answers.
Program
#include <stdio.h>
/* Recursive: x^n = x * x^(n-1), and x^0 = 1 */
double power_recursive(double x, int n) {
if (n == 0) {
return 1;
}
return x * power_recursive(x, n - 1);
}
/* Non-recursive: multiply x by itself n times */
double power_iterative(double x, int n) {
double result = 1;
for (int i = 1; i <= n; i++) {
result *= x;
}
return result;
}
int main(void) {
double x;
int n;
printf("Enter the base x: ");
if (scanf("%lf", &x) != 1) {
printf("Invalid input\n");
return 1;
}
printf("Enter the exponent n (n >= 0): ");
if (scanf("%d", &n) != 1) {
printf("Invalid input\n");
return 1;
}
if (n < 0) {
printf("The exponent must not be negative\n");
return 1;
}
printf("%g ^ %d using recursion = %g\n", x, n, power_recursive(x, n));
printf("%g ^ %d using non-recursion = %g\n", x, n, power_iterative(x, n));
return 0;
}Output
Whole-number base
Enter the base x: 2
Enter the exponent n (n >= 0): 10
2 ^ 10 using recursion = 1024
2 ^ 10 using non-recursion = 1024Fractional base
Enter the base x: 1.5
Enter the exponent n (n >= 0): 3
1.5 ^ 3 using recursion = 3.375
1.5 ^ 3 using non-recursion = 3.375Exponent 0
Enter the base x: 7
Enter the exponent n (n >= 0): 0
7 ^ 0 using recursion = 1
7 ^ 0 using non-recursion = 1Recorded by compiling and running this exact program. Typed input appears after each prompt.
How it works
The syllabus does not say whether n can be negative, so this program takes n ≥ 0. For a negative n you could use x⁻ⁿ = 1 / xⁿ.
x is a double so that bases like 1.5 work. %g prints a double without extra zeros, so 1024 prints as 1024 and 3.375 as 3.375. (%g shows at most 6 significant digits; use %f for very large results.)
For 2¹⁰ the recursive calls go down from n = 10 to n = 0, then each call multiplies the result by 2 on the way back up.
Common mistakes
Writing
x ^ nin C to mean power.^is the bitwise XOR operator, so2 ^ 10gives 8, not 1024.Using the base case
if (n == 1) return x;only. Thenpower(x, 0)never stops recursing. The base case should be n = 0, which returns 1.Starting the result at 0 in the loop. It stays 0 forever. The result must start at 1.
Using
intfor the result. Powers grow very fast, and 2³¹ already overflows a 32-bitint.
Viva questions
Try to answer each one aloud before you open it.
What is x⁰, and why is it the base case?
x⁰ is 1 for any non-zero x. It is the smallest exponent, and its answer is known without any multiplication, so the recursion stops there.
How many multiplications do these functions do for xⁿ?
n multiplications. The recursive version also makes n + 1 function calls.
What does the ^ operator do in C?
It is bitwise exclusive OR (XOR). C has no power operator; you use a loop, recursion or the
powfunction frommath.h.How can xⁿ be found with fewer multiplications?
By repeated squaring: if n is even, xⁿ = (x^(n/2))², so the problem size halves each step and only about log₂ n multiplications are needed.
What is the difference between recursion and iteration?
Recursion repeats work by a function calling itself; iteration repeats it with a loop. Recursion uses stack memory for each call, while a loop uses the same variables throughout.