How to make a loop of a calculator that keeps asking until the input is zero/nothing?

2 views (last 30 days)
Your solution must prompt user to input the option (DR or RD). If the option is DR, then ask for angle in degrees and the program displays the equivalent radians. If the option is RD then ask for angle in radians then the program displays the equivalent degrees. Your program should able to accept both scalar and matrix input. The script should keep running until no number is provided to convert. [The function isempty will be useful here]
i only got this far and there seems to be a problem with my code (when you type a number, it shows 2-4 answer)
choice = menu ('Choose the conversion you want to calculate','DR','RD','Other');
if choice == 1;
fprintf('You have choosen a Degree to Radian conversion');
fprintf('\n');
num = input ('Enter an angle in degrees: ', 's');
result = (num) * pi / 180;
no = str2double(result);
elseif choice == 2;
fprintf('You have choosen a Radian to Degree conversion');
fprintf('\n');
num = input ('Enter a number in Radian: ', 's');
result = (num) * 180 / pi;
no = str2double(result);
else
fprintf('Error mag\n');
end
fprintf('The result of the conversion is %d\n', no);

Accepted Answer

Rik
Rik on 24 Nov 2020
You forgot to convert the result to a number, so it is a char array.
%radian = input ('Input a number: ', 's');
%Input a number: 1.2
% this result in:
radian='1.2';
radian=str2double(radian);
  6 Comments
Ken L
Ken L on 2 Feb 2021
Hi Rik, since this question require isempty function, may I know where to insert the isempty function? Thank you.
Rik
Rik on 2 Feb 2021
What do you think? Your program should keep going until the user doesn't provide an input. How would you detect that?

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!