# Re: Calculating exponents without library functions

**From:** David (*d_pal_at_shaw.ca*)

**Date:** 04/12/04

**Next message:**Bryan Parkoff: "Re: Friend in Class Can't Access Private Function"**Previous message:**Christopher Benson-Manica: "Re: Question"**In reply to:**Mike Wahler: "Re: Calculating exponents without library functions"**Next in thread:**Mike Wahler: "Re: Calculating exponents without library functions"**Reply:**Mike Wahler: "Re: Calculating exponents without library functions"**Messages sorted by:**[ date ] [ thread ] [ subject ] [ author ]

Date: Mon, 12 Apr 2004 02:20:39 GMT

Thanks a lot Mike for the solution

I am trying to do the the calculation without the use of built in pwr

function.I have to use the for or while loop with probably the counter

Thanks once again

David

"Mike Wahler" <mkwahler@mkwahler.net> wrote in message

news:8qlec.5596$k05.3740@newsread2.news.pas.earthlink.net...

*> "David" <d_pal@shaw.ca> wrote in message
*

*> news:7Ujec.78302$Pk3.72421@pd7tw1no...
*

*> > How can you write a program in C++ by using loop to control the
*

*> calculation
*

*> > without the use of library function pwr .For example 3 to the power of 4
*

*> (ie
*

*> > 3x3x3x3). Any help will be appreciated
*

*>
*

*> #include <iostream>
*

*>
*

*> unsigned int pwr(unsigned int base, unsigned int exp)
*

*> {
*

*> unsigned int result = 1;
*

*>
*

*> while(exp--)
*

*> result *= base;
*

*>
*

*> return result;
*

*> }
*

*>
*

*> int main()
*

*> {
*

*> std::cout << pwr(3, 4) << '\n';
*

*> return 0;
*

*> }
*

*>
*

*>
*

*> -Mike
*

*>
*

*>
*

**Next message:**Bryan Parkoff: "Re: Friend in Class Can't Access Private Function"**Previous message:**Christopher Benson-Manica: "Re: Question"**In reply to:**Mike Wahler: "Re: Calculating exponents without library functions"**Next in thread:**Mike Wahler: "Re: Calculating exponents without library functions"**Reply:**Mike Wahler: "Re: Calculating exponents without library functions"**Messages sorted by:**[ date ] [ thread ] [ subject ] [ author ]