![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
powers in c++
How do i make a number to a power in C++? Like 5^2 5 raised to the power 2.
|
|
|
|
|
|
#2 |
|
Programming Guru
![]() ![]() ![]() |
__________________
http://jasonpowers.net "There are a thousand hacking at the branches of evil to one who is striking at the root." |
|
|
|
|
|
#3 |
|
Professional Programmer
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3
![]() |
#include <math.h> ... // 5^2 int num = pow (5,2); EDIT: IR was quicker then me lol
__________________
I am Addicted to Linux! |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Yeah he was quicker, and in C++ one includes cmath and uses the std namespace.
So: #include <iostream> // cout and endl
#include <cmath> // pow
using namespace std;
int main()
{
// 5^2
double num = pow (5,2);
cout << num << endl;
return 0;
}
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates |
|
|
|
|
|
#5 |
|
Professional Programmer
Join Date: Feb 2005
Posts: 346
Rep Power: 4
![]() |
thanks everyone
|
|
|
|
|
|
#6 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
you could make ur own pow function.. and even fix it up for powers of fractions...
double result = 1;
double number = 5;
for (int i = 0 ; i<2 ; i++)
{
result = result*number;
}
cout<<result<<endl;EDIT: im just messing around.. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
:eek:
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|