## Display negative results with "i" rather than negative sign

### Leanne Dozier (view profile)

on 17 Nov 2019 at 5:48
Latest activity Commented on by KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 17 Nov 2019 at 6:37

### KALYAN ACHARJYA (view profile)

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 (view profile)

on 17 Nov 2019 at 5:54
Edited by KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 17 Nov 2019 at 5:56

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

KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 17 Nov 2019 at 6:29
Watever you want you can display, is this?
a=double(n<0)*abs(n)^(1/r)*i+double(n>0)*(n)^(1/r)
use
format shortG
at the beginning of the code to avoid extra decimal3.0000i points
Leanne Dozier

### Leanne Dozier (view profile)

on 17 Nov 2019 at 6:34
That one is much better! Thank you so much!! It's been driving me crazy for hours! I appreciate it!
KALYAN ACHARJYA

### KALYAN ACHARJYA (view profile)

on 17 Nov 2019 at 6:37
It's my pleasure @Leanne Dozier