---
title: "exp(x,y) using recursion"
subject: "Computer Programming"
chapter: 6
key: "semester_1/computer_programming:exp-xy-recursive"
source: https://www.bctengineer.com/q/semester_1/computer_programming/exp-xy-recursive
site: BCT Engineering
---

# exp(x,y) using recursion

Computer Programming · Chapter 6: User-Defined Functions

- Asked in: 2069 Chaitra, 4 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_1/computer_programming/exp-xy-recursive
- Whole chapter: https://www.bctengineer.com/md/semester_1/computer_programming/chapter-6

## Question

Write a program to calculate the exp(x,y) using recursive function without using pow() function. [Example: exp(2,3)=2^3 i.e. 2*2*2=8.]

## Short answer

Recursive power: if y==0 return 1, else return x * exp_xy(x, y-1). For exp(2,3), the result is 8.

## Answer

**Idea.** x^y = x × x^(y−1), and x^0 = 1. The function calls itself with y reduced by one until y becomes 0, then the products are returned back one by one.

For exp(2,3): 2 × exp(2,2) = 2 × (2 × exp(2,1)) = 2 × (2 × (2 × exp(2,0))) = 2 × 2 × 2 × 1 = 8.

```c
#include <stdio.h>

/* x raised to y (y >= 0) by recursion */
long exp_xy(int x, int y)
{
    if (y == 0)                 /* base case: x^0 = 1 */
        return 1;
    return x * exp_xy(x, y - 1);
}

int main(void)
{
    int x, y;
    printf("Enter x and y: ");
    scanf("%d %d", &x, &y);
    if (y < 0)
        printf("y must not be negative\n");
    else
        printf("exp(%d,%d) = %ld\n", x, y, exp_xy(x, y));
    return 0;
}
```

**Sample run**

Input: `2 3`

```text
Enter x and y: exp(2,3) = 8
```

The function handles y ≥ 0 only (a negative power would need 1/x^(−y) and a floating-point return type).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. Which relation does exp_xy use?
   A. x^y = x + x^(y-1)
   B. x^y = y * x^(x-1)
   C. x^y = x * x^(y-1)
   D. x^y = x^(y/2) + x^(y/2)
   Answer: C. The power is the base multiplied by the same base to one lower power. (Key point: Recursive relation)

2. What is the base case and why?
   A. y == 0 returns 1, since x^0 = 1
   B. y == 0 returns 0, since nothing is multiplied
   C. y == 1 returns 0
   D. x == 0 returns 1
   Answer: A. Any number to the power 0 is 1, which is the multiplicative identity for the chain. (Key point: Base case)

3. How is exp(2,3) evaluated?
   A. 2 + 2 + 2 = 6
   B. 2 * 3 = 6
   C. 2 * (2 * (2 * 1)) = 8
   D. 3 * (3 * 3) = 27
   Answer: C. Products are returned back one at a time after reaching exp(2,0) = 1. (Key point: Trace of exp(2,3))

## Related in Computer Programming

- [Correct program: do-while and pow](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Acorrect-program-sum-root) — Computer Programming, chapter 3
- [Recursive function and how it works](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Arecursive-function-explain) — Computer Programming, chapter 6, asked 3×
- [Series 1+11+111+… by recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aseries-1-11-111-recursive) — Computer Programming, chapter 6
- [Sum of digits using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Asum-digits-recursive) — Computer Programming, chapter 6
- [Armstrong number using recursion](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Aarmstrong-recursive) — Computer Programming, chapter 6

## Related in other subjects

- [Is recursion slow; recursive vs non-recursive](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Arecursion-slow-compare) — Data Structure and Algorithm, chapter 5
- [Function template power(base, exp)](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Apower-function-template) — Object Oriented Programming, chapter 9
- [Recursion; iterative vs recursive choice](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Adefine-recursion-iterative-vs-recursive) — Data Structure and Algorithm, chapter 5
- [Normal vs recursive function](https://www.bctengineer.com/question.md?key=semester_4%2Fdata_structure_and_algorithm%3Anormal-vs-recursive-function) — Data Structure and Algorithm, chapter 5
