Write C++ program to create class power which contains function to find power of given number.

//Note: If you are not using Borland or Turbo C then add the following line after header files :
//using namespace std;

#include<conio>
#include<iostream>
class power
{
   public:
   int pow(int n, int p)
  {
      if(p==0)
      {
      return 1;
       }
       else
       return n*pow(n,p-1); // recursion function
   }
};
int main()
{
   int n,f,p;
   cout<<"Enter number:";
   cin>>n;
   cout<<endl;
   cout<<"Enter power of number:";
   cin>>p;
   cout<<endl;
   power obj; // object created
   cout<<"The power of "<<n<<" is: "<<obj.pow(n,p);
   getch();
   return(0);
}

0 Comments