Sunday, 15 June 2014

C++ program to calculate power of a number

Here are seven different C++ programs to calculate power of a number like using pow() function, powf() function, without using any in-built function, through recursion, using operator overloading etc.
 


C++ program to calculate power of a number using  pow() function

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    double number,power;
    double num2;
    cout<<"Enter number : ";
    cin>>number;
    cout<<"Enter the power of number : ";
    cin>>power;
    num2=pow(number,power);
    cout<<number<<"^"<<power<<" = "<<num2;
    return 0;
}



C++ program to calculate power of a number using  powf() function

#include<iostream>
#include<math.h>
using namespace std;
int main()
{
    float number,power;
    float num2;
    cout<<"Enter number : ";
    cin>>number;
    cout<<"Enter the power of number : ";
    cin>>power;
    num2=powf(number,power);
    cout<<number<<"^"<<power<<" = "<<num2;
    return 0;
}



C++ program to calculate power of a number using  powl() function

#include<iostream>
#include<math.h>
#include<iomanip>
using namespace std;
int main()
{
    long double number,power;
    long double num2;
    cout<<"Enter number : ";
    cin>>number;
    cout<<"Enter the power of number : ";
    cin>>power;
    num2=powl(number,power);
    cout<<number<<"^"<<power<<" = "<<setprecision(14)<<num2;
    return 0;
}



C++ program to calculate power of a number without using inbuilt functions

#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
    long double number,power,num2;
    cout<<"Enter number : ";
    cin>>number;
    num2=number;
    cout<<"Enter the power of number : ";
    cin>>power;
    for(int i=1;i<power;i++)
        number*=num2;
    cout<<num2<<"^"<<power<<" = "<<setprecision(14)<<number;
    return 0;
}



C++ program to calculate power of a number using function

#include<iostream>
#include<iomanip>
using namespace std;
long double power(long double num,long double pow)
{
    long double temp=num;
    while(pow>1)
    {
        num*=temp;
        pow--;
    }
    return num;
}
int main()
{
    long double number,pow,num2;
    cout<<"Enter number : ";
    cin>>number;
    cout<<"Enter the power of number : ";
    cin>>pow;
    num2=power(number,pow);
    cout<<number<<"^"<<pow<<" = "<<setprecision(14)<<num2;
    return 0;
}



C++ program to calculate power of a number using recursion

#include<iostream>
#include<iomanip>
using namespace std;
long double power(long double num,long double pow,long double temp)
{
    if(pow==1)
        return num;
    return power(num*temp,pow-1,temp);
}
int main()
{
    long double number,pow,num2;
    cout<<"Enter number : ";
    cin>>number;
    cout<<"Enter the power of number : ";
    cin>>pow;
    num2=power(number,pow,number);
    cout<<number<<"^"<<pow<<" = "<<setprecision(14)<<num2;
    return 0;
}



C++ program to calculate power of a number using operator overloading

#include<iostream>
#include<iomanip>
using namespace std;
struct pow{
  long double num;
};
long double operator^(pow &a,int power)
{
    long double number=a.num;
    for(int i=1;i<power;i++)
        number*=a.num;
    return number;
}
int main()
{
    pow a;
    int power;
    long double number;
    cout<<"Enter number : ";
    cin>>a.num;
    cout<<"Enter the power of number : ";
    cin>>power;
    number=a^power;
    cout<<a.num<<"^"<<power<<" = "<<setprecision(14)<<number;
    return 0;
}



C++ program to calculate power of a number using class

#include<iostream>
#include<iomanip>
using namespace std;
class pow
{
private:
    long double number;
    int power;
public:
    void getdata(long double i,int j)
    {
        number=i;
        power=j;
    }
    long double findpow()
    {
        long double num2=number;
        while(power>1)
        {
            number*=num2;
            power--;
        }
        return number;
    }
};
int main()
{
    pow obj;
    long double num,number;
    int p;
    cout<<"Enter number : ";
    cin>>num;
    cout<<"Enter the power of number : ";
    cin>>p;
    obj.getdata(num,p);
    number=obj.findpow();
    cout<<num<<"^"<<p<<" = "<<setprecision(14)<<number;
    return 0;
}

No comments:

Post a Comment