Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 18th, 2006, 10:21 AM   #1
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
powers in c++

How do i make a number to a power in C++? Like 5^2 5 raised to the power 2.
cwl157 is offline   Reply With Quote
Old Apr 18th, 2006, 10:36 AM   #2
Infinite Recursion
Programming Guru
 
Infinite Recursion's Avatar
 
Join Date: Jul 2004
Location: United States
Posts: 3,473
Rep Power: 8 Infinite Recursion is on a distinguished road
Send a message via MSN to Infinite Recursion Send a message via Yahoo to Infinite Recursion
The POW function.

http://www.cplusplus.com/ref/cmath/pow.html
__________________
http://jasonpowers.net

"There are a thousand hacking at the branches of evil to one who is striking at the root."
Infinite Recursion is offline   Reply With Quote
Old Apr 18th, 2006, 10:38 AM   #3
King
Professional Programmer
 
King's Avatar
 
Join Date: Jan 2006
Location: Ontario, Canada
Posts: 380
Rep Power: 3 King is on a distinguished road
#include <math.h>
...
// 5^2
int num = pow (5,2);

EDIT: IR was quicker then me lol
__________________
I am Addicted to Linux!
King is offline   Reply With Quote
Old Apr 18th, 2006, 10:48 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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
nnxion is offline   Reply With Quote
Old Apr 18th, 2006, 6:50 PM   #5
cwl157
Professional Programmer
 
Join Date: Feb 2005
Posts: 346
Rep Power: 4 cwl157 is on a distinguished road
thanks everyone
cwl157 is offline   Reply With Quote
Old Apr 18th, 2006, 6:57 PM   #6
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3 hbe02 is on a distinguished road
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..
hbe02 is offline   Reply With Quote
Old Apr 18th, 2006, 7:11 PM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
: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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 2:13 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC