计算没有库函数的指数 [英] Calculating exponents without library functions
问题描述
如何在C ++中编写程序,使用循环来控制计算
而不使用库函数pwr。例如3到4的幂(即
3x3x3x3)。任何帮助将不胜感激
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
推荐答案
" David" < d _ *** @ shaw.ca>在消息中写道
news:7Ujec.78302
"David" <d_***@shaw.ca> wrote in message
news:7Ujec.78302
Pk3.72421@pd7tw1no ...
Pk3.72421@pd7tw1no...
你怎么能写一个程序C ++通过使用循环来控制
计算而不使用库函数pwr。例如3到4
(即3x3x3x3)的幂。任何帮助将不胜感激
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--)
结果* =基数;
返回结果;
}
int main()
{
std :: cout<< pwr(3,4)<< ''\ n'';
返回0;
}
-Mike
#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
非常感谢Mike的解决方案
我正在尝试进行计算而不使用内置的pwr
函数。我必须可能是柜台使用for或while循环
再次感谢
David
Mike Wahler < MK ****** @ mkwahler.net>在消息中写道
news:8q ***************** @ newsread2.news.pas.earthl ink.net ...
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" <mk******@mkwahler.net> wrote in message
news:8q*****************@newsread2.news.pas.earthl ink.net...
大卫 < d _ *** @ shaw.ca>在消息中写道
新闻:7Ujec.78302
"David" <d_***@shaw.ca> wrote in message
news:7Ujec.78302
这篇关于计算没有库函数的指数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!