Solved

Exponents in C#

Posted on 2003-11-18
4
3,522 Views
Last Modified: 2008-10-12
How do you use Exponents in C#?  It doesnt seem to have a Math library.
0
Comment
Question by:pmesick12345
  • 2
4 Comments
 
LVL 7

Assisted Solution

by:jj819430
jj819430 earned 20 total points
ID: 9773045
I would just create a routine to do it.
You can do it any way you want, with recursion or what not.

// this is UNTESTED code. May have errors
public int power(int base, int power)
{
if(power == 1)
{
return base;
}

return base * power(base, power-1);
}
0
 
LVL 4

Accepted Solution

by:
wile_e_coyote earned 30 total points
ID: 9773634
using System.Math;

...

int x = 4
int y = pow (x, 3);    // returns x raised to the 3rd power
0
 

Author Comment

by:pmesick12345
ID: 9774184
it is simply Math.pow(base, exponent)
I didnt have to include anything extra in the "using" section
0
 
LVL 7

Expert Comment

by:jj819430
ID: 9774195
include Math.h most likely
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

825 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question