1
3.6kviews
Write a program using recursive function 'power' to compute $x^n$

power(x, n) = 1 if n = 0

power(x,n) = x if n – 1

power(x,n) = x*power(x, n - 1) otherwise


Mumbai university > FE > SEM 2 > Structured Programming Approach

Marks: 10M

Year: May 2014

1 Answer
1
266views

Algorithm:

Main () Function:

  • Start.
  • Print “Enter values of x and n”.
  • Input x, n.
  • r = CALL power (arguments: x, n)
  • Print r.
  • Stop.

Exponential (parameters: x, n)

  • Start.
  • If n=0, then return 0.
  • Else if n =1, then return x.
  • Else return(x*CALL exponential(arguments: x, n-1))
  • Stop.

Program:

#include <stdio.h>
#include <conio.h>
int main(){
    int r, n, x;
    int power(int x, int n);
    printf("Enter a Base no:");
    scanf("%d", &x)
    printf("Enter a Power:");
    scanf("%d", &n);
    r=power(x, n);
    printf("x^n=%d", r);
    getch();
}
int power(int x, int n) {
    if(n==0)
    return 1;
    else if (n==1)
    return x;
    else
    return (x*power(x, n-1));
}

Output:

Enter a Base no: 5

Enter a Power: 3

x^n = 125

Please log in to add an answer.