Display negative results with "i" rather than negative sign

4 次查看(过去 30 天)
Hello,
I'm fairly new to Matlab and I think the solution to this is something to do with fprintf, but I don't know for sure. My assignment is to write a program to determine the nth root between 2<n<99 and any number between -10,000,000 and 10,000,000. I'm not allowed to use Matlab's SQRT function. For negative numbers they have to be displayed using an i rather than a negative sign (ex. 3i not -3). The instructor never said we couldn't use the nthroot function, but just in case I wrote two sets if code very similar. On the one where I take a number to the fractional power (ex. 81^1/2 =9) all is well until I put in a negative number. Then I get two results. Then on the other program using the nthroot function the answer is displayed as a negative rather than i and I can't figure out how to convert these answers. Here is my code. Can anyone help? Thanks in advance
Program 1:
n=input ('enter a number between -10,000,000 and 10,000,000: ');
if n<-10000000 || n>10000000
disp('error on input number');
pause
exit
end
r=input ('enter the nthroot to be calculated: ');
if r<2 || r>99
disp('error on input number');
pause
exit
end
rr=(1/r);
a=n^rr
Program 2:
n=input ('enter a number between -10,000,000 and 10,000,000: ');
if n<-10000000 || n>10000000
disp('error on input number');
pause
exit
end
r=input ('enter the nth root to be calculated: ');
if r<2 || r>99
disp('error on input number');
pause
exit
end
a=nthroot(n,r)

采纳的回答

KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-17
编辑:KALYAN ACHARJYA 2019-11-17
Is this: First one, if OK, do the same for 2nd program
n=input ('enter a number between -10,000,000 and 10,000,000: ');
if n<-10000000 || n>10000000
disp('error on input number');
else
r=input ('enter the nthroot to be calculated: ');
if r<2 || r>99
disp('error on input number nthroot');
else
rr=(1/r);
a=n^rr
end
end
  6 个评论
Leanne Dozier
Leanne Dozier 2019-11-17
That one is much better! Thank you so much!! It's been driving me crazy for hours! I appreciate it!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arithmetic Operations 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by