03-02-2005, 08:24 AM
Quote:Ah, yes, he does say they must be whole numbers.Yes, the laws of logs make for a good solution.
New program:
[syntax="QBASIC"]
cls
b% = -1
num% = -1
while (b% <= 0) or (num% <= 0)
line input "Number: ", num$
line input "Base: ", b$
num% = int(val(num$))
b% = int(val(b$))
if b% <= 0 then
print "Invalid base; must be greater than zero."
elseif num% <= 0 then
print "Invalid number; must be greater than zero."
end if
wend
' calculate the exponent.
exponent! = log(num%) / log(b%)
if exponent! <> fix(exponent!) then
print num$ + " is not an integer power of " + b$
else
print num$ + " is a power of " + b$ + " (exponent=" + ltrim$(str$(int(exponent!))) + ")"
end if
end
[/syntax]
Sample:-shiftLynxCode:Number: 78125
Base: 5
78125 is a power of 5 (exponent=7)
However, the output of your entry does not look like the sample above. It does not say if in fact it is a power of the base number. It just displays the exponent, which could have decimals when it is NOT a power.
Also, you input the values into strings and convert them to integers. If an input has a non-numeric character, the VAL will just ignore it and proceed with whatever is left. Not very clean code.
If the A value is greater that 1 and the B value is 1, you get a error of "division by zero".
All in all, your solution needs more work.
*****