Tuesday, November 20, 2012

C++: calculate power (function(

#include<iostream.h>
int power(int x,int y)
{
    int k=x;
    if(y==0)
        return 1;
    else
        for(int i=1;i<y;++i)
            x=x*k;
        return x;
}
int main()
{
    int a,b,c;
    cout<<"enter the number:";
    cin>>a;
    cout<<"enter the power:";
    cin>>b;
    c=power(a,b);
    cout<<"the result is:"<<c<<endl;
    return 0;
}

No comments:

Post a Comment