// Write a program to find x^y using recursion
#include<iostream.h>
#include<conio.h>
float expo(float a,int n)
{
if(a==0)
return 0.0;
else if(n==0)
return 1.0;
else if(n>0)
return(a*expo(a,n-1));
else
return(1/a*expo(a,n+1));
}
void main()
{
float expo(float,int);
float x;
int y;
clrscr();
cout<<"Enter the base: ";
cin>>x;
cout<<"Enter the exponent: ";
cin>>y;
cout<<x<<"^"<<y<<"="<<expo(x,y);
getch();
}

No comments: