help raising number to powers

Hey guys, i can't figure this out. I have a c++ book but it doesn't really help me on this...

I need to compute things such as

3^2 = 9 (three squared = 9)
5^2 = 25 (5 squared = 25)

e^2 = 7.389 (e on calculator)
e ^ 1/2 = 1.64

here is what I have:, but it's not working

#include<iostream>
using namespace std;

int main()

double answer;
double x;

cin>>x;
answer = (x^2);
cout<<answer;

return 0;
}



basically I want to be able to replace "answer" with functions like i mentioned above.
Also, for proper use of "^", read
http://www.cplusplus.com/doc/tutorial/operators/
you can write it like,
1
2
3
4
answer=pow(x,2);
answer=pow(x,3);
answer=pow(x,4);
//..etc 

its a ready-made function, but you must include cmath library in order to use it..
#include<cmath>

OR you can compute the answer by looping:

1
2
for(int i=2, i>1 ,i--) //change the value of i depends on the exp 2,3,4, etc.
answer*=answer; //equiv to answer=answer*answer; 


OR

1
2
3
4
5
6
int i=2;
while(i>1)
{
           answer*=answer;
           i--;
}


or a Recursive function when you get to that.
make sure to include
#include<cmath>

before using the pow operataor

z= pow(x,y);

means z = to x raised to the power of y
Topic archived. No new replies allowed.