Introduction In this i have made use of recursion to calculate x to the power n Code: #include <stdio.h> main() { float x; int n; printf("\n enter values of x an n"); printf("\n %.4f to the power of % d is %.4f",x,n,power(x,n)); } Here power is a user defined function the job of this function is to check the values of x and n and either call the function recursively or return back. Code: float power(float x, int n) { if (x==0) { return 0; } else if(n==0) { return 1; } else if (n>0) { return( x* power(x,n-1)); } else { return ((1/x)*power(x,n+1)); } }
This is a simple implementation of a standard algorithm & is a classic example of Tail Recursion - the form of recursion which is redundant & can be replaced by a simpler, more efficient iterative equivalent (as Pradeep has done in his article of the same program - click here) Regards, Rajiv Iyer PS: Tail Recursion's classic trademark identification signature is that the recursive calls are usually the last statements within the recursive function's body.