0
730views
What is recursion?

Write a program to find $x^y$ using recursion.

Subject : Structured Programming Approach

Title : Functions and Parameter

Difficulty : Medium

1 Answer
0
0views

Recursion is a programming technique in which function calls itself again and again till some logical condition is satisfied.

#include<stdio.h>
#include<conio.h>

double pow1(double, double); // function prototype

int main() {
    double b, p;
    int e;
    clrscr();

    printf("\n\n\t Please enter base");
    scanf("%lf", &b);

    printf("\n\n\t Please enter exponent");
    scanf("%d", &e);

    p = pow1(b,e); // function call

    printf("\n\n\t Power of %lf to %d = %lf", b,e,p);

    getch();
    return 0;
}

double pow1(double b, int e) { // declaration
    if(e == 0)
        return 1;
    else if(e > 0)
        return b * pow1(b, e-1);
    else
        return 1 / pow1(b, -e);
}
Please log in to add an answer.