Display negative results with "i" rather than negative sign

4 views (last 30 days)
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)

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 17 Nov 2019
Edited: KALYAN ACHARJYA on 17 Nov 2019
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 Comments
Leanne Dozier
Leanne Dozier on 17 Nov 2019
That one is much better! Thank you so much!! It's been driving me crazy for hours! I appreciate it!

Sign in to comment.

More Answers (0)

Categories

Find more on Arithmetic Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!