Pages

Saturday, June 29, 2013

Recursive power function in C++

/*
QUESTION:

WRITE a recursive function int Power(int a , int b) that computes the product of two integers a  and b. The only arithmetic operation that you are allowed is "+"(addition)
*/

// CODE:

#include <iostream>
using namespace std;

int Power(int a,int b);
//MAIN STARTS
int main(){
int a,b;
cout<<"ENTER the NUmber : ";
cin>>a;

cout<<"\nEnter the power of number : ";
cin>>b;

cout<<"\n\nREsult is :"<<Power(a,b)<<endl;

 return 0;
}


//MAIN ENDS
int Power(int a,int b){
if (b==0)
return 1;
if (a == 0)
return 0;

else
return ( a * Power(a,b-1) );

}

No comments:

Post a Comment