Display negative results with "i" rather than negative sign

4 visualizaciones (últimos 30 días)
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)

Respuesta aceptada

KALYAN ACHARJYA
KALYAN ACHARJYA el 17 de Nov. de 2019
Editada: KALYAN ACHARJYA el 17 de Nov. de 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 comentarios
Leanne Dozier
Leanne Dozier el 17 de Nov. de 2019
That one is much better! Thank you so much!! It's been driving me crazy for hours! I appreciate it!
KALYAN ACHARJYA
KALYAN ACHARJYA el 17 de Nov. de 2019
It's my pleasure @Leanne Dozier

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Arithmetic Operations en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by