where m is nonnegative integer
How can i compute 10 to power m using for loop in c++ (without using function power) ?
Here is pseudocode
temp =1
for a = 1 to m
temp =10*temp
end
This is a simple question about the nature of a power.
if you wanted to use the function exp you could also do it well
y = 10^m
log(y) = m*log(10)
y=exp(m*log(10))
its a one-liner and a cheap shot. If your teacher wants you to show a loop and an understanding of how loops and the definition of a power works, this isnt going to do it.
If you are looking for a fast one-liner from a standard (optimized) library then this might work.
Depends on why you want it.
Reply:y = 10
for 1 to m
{y = y*10
m = m + 1}
endfor
something to that effect
Reply:10 to power m (denoted 10^m here), is the same as 10 multiplied by itself in a loop that lasts for m cycles. 10 is unique because 10^m is the same as 1 with m zeros behind it - so you can also use a loop to just keep adding zeros to a print output.
Reply:long int r = 1;
for(long int x=0;x%26lt;=9;x++){
r *= 10;
}
Change to adjust to what power you want to raise 10 to.
r must be one, I used long because int can sometimes be too short since the furthest it could calculate is 10 to the 4th power.
Reply:Here is some pseudocode that you may find helpful:
result = 1
for i=1 to m
result = result * 10
end for
output result
Alternately, you could do it by means of a recursive function and avoid a loop altogether.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment