How do I raise a number to a power?

```
2^1
2^2
2^3
```

etc…

pow() in the cmath library. More info here.

Don’t forget to have the #include at the top.

`std::pow`

in the `<cmath>`

header has these overloads:

```
pow(float, float);
pow(float, int);
pow(double, double); // taken over from C
pow(double, int);
pow(long double, long double);
pow(long double, int);
```

Now you can’t just do

```
pow(2, N)
```

with N being an int, because it doesn’t know which of float, double or long double version it should take, and you would get an ambiguity error. All three would need a conversion from int to floating point, and all three are equally costly!

Therefor, be sure to have the first argument typed so it matches one of those three perfectly. I usually use `double`

```
pow(2.0, N)
```

Some lawyer crap from me again. I’ve often fallen in this pitfall myself, so i’m going to warn you about it.

Use the pow(x,y) function: See Here

Just include math.h and you’re all set.

You should be able to use normal C methods in math.

`#include <cmath>`

`pow(2,3)`

if you’re on a unix-like system, `man cmath`

Is that what you’re asking?

Sujal

While `pow( base, exp )`

is a great suggestion, be aware that it typically works in floating-point.

This may or may not be what you want: on some systems a simple loop multiplying on an accumulator will be faster for integer types.

And for square specifically, you might as well just multiply the numbers together yourself, floating-point or integer; it’s not really a decrease in readability (IMHO) and you avoid the performance overhead of a function call.

In C++ the “^” operator is a bitwise OR. It does not work for raising to a power. The x << n is a left shift of the binary number which is the same as multiplying x by 2 n number of times and that can only be used when raising 2 to a power. The POW function is a math function that will work generically.

I don’t have enough reputation to comment, but if you like working with QT, they have their own version.

```
#include <QtCore/qmath.h>
qPow(x, y); // returns x raised to the y power.
```

Or if you aren’t using QT, cmath has basically the same thing.

```
#include <cmath>
double x = 5, y = 7; //As an example, 5 ^ 7 = 78125
pow(x, y); //Should return this: 78125
```

```
#include <iostream>
#include <conio.h>
using namespace std;
double raiseToPow(double ,int) //raiseToPow variable of type double which takes arguments (double, int)
void main()
{
double x; //initializing the variable x and i
int i;
cout<<"please enter the number";
cin>>x;
cout<<"plese enter the integer power that you want this number raised to";
cin>>i;
cout<<x<<"raise to power"<<i<<"is equal to"<<raiseToPow(x,i);
}
```

//definition of the function raiseToPower

```
double raiseToPow(double x, int power)
{
double result;
int i;
result =1.0;
for (i=1, i<=power;i++)
{
result = result*x;
}
return(result);
}
```

It’s pow or powf in `<math.h>`

There is no special infix operator like in Visual Basic or Python

```
pow(2.0,1.0)
pow(2.0,2.0)
pow(2.0,3.0)
```

Your original question title is misleading. To just *square*, use `2*2`

.

```
int power (int i, int ow) // works only for ow >= 1
{ // but does not require <cmath> library!=)
if (ow > 1)
{
i = i * power (i, ow - 1);
}
return i;
}
cout << power(6,7); //you can enter variables here
```

Note that the use of pow(x,y) is less efficient than x*x*x y times as shown and answered here https://stackoverflow.com/a/2940800/319728.

So if you’re going for efficiency use x*x*x.

I am using the library `cmath`

or `math.h`

in order to make use of the `pow()`

library functions that takes care of the powers

```
#include<iostream>
#include<cmath>
int main()
{
double number,power, result;
cout<<"\nEnter the number to raise to power: ";
cin>>number;
cout<<"\nEnter the power to raise to: ";
cin>>power;
result = pow(number,power);
cout<<"\n"<< number <<"^"<< power<<" = "<< result;
return 0;
}
```